How to Undo Last Commit in Git

Abdul Jabbar Feb 02, 2024
  1. Undo Last Commit in Git (Long Version)
  2. Undo Last Commit in Git (Short Version)
How to Undo Last Commit in Git

While working with Git we face various situations where we deliberately want to undo the last commit because we would like to recommit it extensively or even remove it completely together due to mistakes we made in the past. Just think how many more people would use Git if it had simple Undo Last Commit options or even a keyboard shortcut that would be the most popular feature.

We have a straightforward and powerful command that does the same as discussed above. That command is really powerful, as it discards the last commit and restores the HEAD (the current branch) to the state it was before the last commit was made in that repository. It may be somewhat confusing for new Git Users but once we get our hands on it, it will become so easy for our Git toolbox.

Undo Last Commit in Git (Long Version)

First, open up the desired terminal, and run Git status. This will show us the state of our repository, including the list of all commits that we have made. Most of the time, it says there are three commits.

The first one is a commit to a branch called master, which is the branch we are currently on in the repository. The second one is a commit to a new branch called Dev, and finally, the third one is a commit to yet another branch called NewDev.

The last two are interesting, as they are the ones we want to undo. We want to discard the commit we have made to Dev and NewDev and restore the HEAD to the state it was before those commits were made. Let’s run the below command, and see what happens after that:

$ git reset --hard 

HEAD is now at the previous state where it was previously. As we can see, the new HEAD pointer is now back to the next commit. Note that there is no longer a commit marked as Dev. Also, the commit marked as NewDev is back to next commit.

Undo Last Commit in Git (Short Version)

If we want to undo the last commit, make sure we are on the branch we want to undo the commit from, then run git reset –hard. If we want to undo the last commit on the current branch, make sure we are on master, then run the below command.

git reset --hard origin/master.  

The procedures mentioned above can only be applied when we are completely sure that we don’t need these changes anymore. But for the other way round, if we need these changes as uncommitted local changes in our working copy, then for this case, the reset command is our solution for this problem:

$ git reset --soft HEAD~1

reset will undo our current HEAD branch to a specific point. In our above-mentioned example, we are commanding to return to the last commit before the recent undo - beneficially creating our last commit not done. The --soft flag ensures that the changes in undone revisions are restored.

Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Commit