Select Page

Introduction

Oftentimes in a project, you might wish to make small changes to your most recent git commit. Using the git commit command directly will create a new commit for you which will include the staged changes, but doing this for every small tweak can create a long list of unwanted commits that end up getting harder to keep track of. This is where the –amend flag after the git commit command comes into play. Rather than creating a whole new commit, it instead modifies the very last commit you’ve made, and applies the changes to it. But there’s a slight little problem, that is, not only does this modify the previous commit, but completely replaces the previous commit with a new ref.

Nevertheless, there will also be times when you want to revert back to the previous state of the file and cancel the amendment you just did. Unfortunately, it isn’t as simple as it should be. Small changes should not be much of a hassle, but larger ones may end up being quite the pain. Hopefully, it should at least save you from some terrible damage. So let’s get this done, people!


Use git reflog to locate amendment

Before you can use the command required to revert back to the previous state of your commit, you need to figure out the location or the situation of your previous commit. This can be achieved by using either the “git log” command or the “git reflog” command. The git log command will return you with an output of the full HASH string, which is very, very long, but also exact. On the other hand, using the “git reflog” command will give you a shortened version of the HASH, which itself can be used for the command. So in this guide, we’ll be using git reflog to undo the –amend.

The best way to demonstrate the process would be to actually show you a demo scenario that you might face when using the “git commit –amend” command. Let me start off by creating a demo folder and initialize it using git init:

Now let’s create a file and append some text to it.

Once we’re done creating the file, it’s time to stage the file we just created and then commit it:

Everything has been smooth and fine so far. But wait! I just realized that I need to add some more text to the previous file, but the change is quite small. Creating a whole new commit for it would not be a great idea. But luckily, git has the –amend flag. So why not use it?

That looks pretty good! But wait…

Wasn’t I supposed to write something else instead of this? Man… I messed up.

But not really. This is exactly where things get interesting. Yeah. Trust me. Your previous commit has been replaced, but not entirely. If at this stage, you add no further commits, then you’d still be able to recover the previous commit and undo the change you made by applying the –amend flag. To do this, the first thing you need to do is figure out the HEAD for the previous commit before you applied –amend, by simply using the reflog command in git:

$ git reflog

Aha! I can still see the very first commit, which has been marked as (initial). This commit was under HEAD@{1), and the Hash for that commit seems to have been 8c7721a. With the help of this information, I can now revert to the previous state of my commitment. The next section will tell you just how!


Use git reset to undo ‘git commit –amend’

This is the most important part of the whole article, so pay close attention. Using the information we found from the output of “git reflog”, our very next command will involve using either the HASH or the HEAD to use along with the main command, which is “git reset”. To finally revert back to the previous state of your commit and remove the amendment, you’d want to use any of the following commands. 

The first option is to use the HASH, and the full command that you need to type out and execute in your terminal should look something like:

$ git reset <HASH> --soft

In my case, the HASH was 8c7721a, so I’d use this command to revert back to the previous state of my commit:

$ git reset 8c7721a --soft

Now if I try using the git status command, look at what I get:

If you’ve understood everything properly all along, you should be able to see that file1.txt has been modified, but not committed. This means that we can now recommit it if we wish, but we have successfully removed the “git commit –amend” commit. After this, you can do whatever changes you like, and finally make a new commit once again.

Also, if you try out the git reflog command at this state, you should be able to observe something like the one below:

Wala! You have now successfully undone the “git commit –amend” command in your git repository!