How do you do undo git reset –hard HEAD^ and revert to previous commit ?

In the previous sneppet Throw away the local commits from your branch in Git you had learnt how to discard local commits from your Git branch using git reset command. In this sneppet, you will learn how to undo git reset and the changes caused using the following command.

git reset --hard HEAD^

As you know, the above git command will throw away any uncommitted changes you have in your branch. And HEAD points to current commit. 

That means, the above command will make your branch pointing back to current commit. Then resets the index and working tree. Any changes made to the tracked files in the working tree from the time of the current commit will be discarded. Any untracked files or directories are also simply deleted.

undo git reset –hard HEAD^ and revert to previous commit

Now, lets see how to undo git reset –hard HEAD^. Let’s say you wanted to go back to some good commit. To do so, first you need to run the following git command and find the commit # that you wanted to revert to and simply run git reset –hard <commit #>

git reflog

Git uses the git reflog command to record the changes that you made to the tips of branches. Also, it allows you to go back the specific commits that are not linked to any branch or tag.

For example,

The content of the file1.txt is modified to update third line.

first line 
second line 
third line

I ran the following commands to commit the changes to my local branch.

git add .

git status

git commit -m "third line"

After this, I ran git reflog command.

$ git reflog

8a1fd3e (HEAD -> main) HEAD@{0}: commit: third line
6cee22e (origin/main, origin/HEAD) HEAD@{1}: reset: moving to 6cee22e
13c3021 HEAD@{2}: reset: moving to HEAD^
6cee22e (origin/main, origin/HEAD) HEAD@{3}: commit: second line
13c3021 HEAD@{4}: commit: undo git reset
5665632 HEAD@{5}: reset: moving to HEAD@{2}
3aff737 HEAD@{6}: reset: moving to HEAD@{0}

git reset to discard local commit

Let’s say after running the following git reset command, now the HEAD is now at 6cee22e.

$ git reset --hard HEAD^

HEAD is now at 6cee22e second line

Now the content of the file is as follows:

first line
second line

Revert to previous commit:

First, you need to run git reflog command and find the good commit or the revisions that you wanted back in your local repository.

$ git reflog

6cee22e (HEAD -> main, origin/main, origin/HEAD) HEAD@{0}: reset: moving to HEAD^
8a1fd3e HEAD@{1}: commit: third line
6cee22e (HEAD -> main, origin/main, origin/HEAD) HEAD@{2}: reset: moving to 6cee22e
13c3021 HEAD@{3}: reset: moving to HEAD^
6cee22e (HEAD -> main, origin/main, origin/HEAD) HEAD@{4}: commit: second line
13c3021 HEAD@{5}: commit: undo git reset
5665632 HEAD@{6}: reset: moving to HEAD@{2}

Note down the commit # of “third line” content commit which is 8a1fd3e, then run the following command to undo reset.

$ git reset --hard 8a1fd3e

HEAD is now at 8a1fd3e third line

Now if you check the file1.txt content, you would notice that git reset –hard HEAD^ changes are undone and you got your changes back.

first line 
second line 
third line

That’s it. Hope you find this sneppet helpful 🙂

You’ll Also Like



Notify of

Inline Feedbacks
View all comments