Home » How to Undo Last Commit in Git

How to Undo Last Commit in Git

Last updated on June 12, 2020 by

Did you accidentally commit the wrong files to Git and you want to undo or revert the commit? In this article, we will show you how to undo or remove the last commit in Git. We hope you love it. Let’s just dive into it.

Table of Contents
1. Undo Last Local Commits in Git
CASE 1.1: Preserve Changes
CASE 1.2: Hard Reset Git Commit
2. Git Revert Pushed or Public Commit
2.1 Using git revert
2.2 Using git reset

01 Undo Last Local Commits in Git

CASE 1.1: Preserve Changes

If you haven’t pushed the commit to the server yet, then you just run the git reset command as shown below and you’re done.

Notes: Please note that following code is safe and will not remove your changes, it will just untracked your the last commit

$ git reset HEAD~

Example:

Let’s see a very simple example for better understanding.

remove last commit from git
  1. We have run the git status command to see the modified files
  2. Then by using git add <filename> or . (dot), we added "accidentally-committed-file.php" on the track
  3. After that, we commit our changes with a message using git commit -m "msg"
  4. We then again ran the git status command to verify the changes and it’s committed to locally. After that, we want to push those changes, but you realize that something went wrong and want to undo those unpublished changes
  5. Then we discovered the git reset HEAD~ command and we ran this command and we got what you wanted.

Notes: Use git reset --soft HEAD~ when you want to add additional changes to the previous commit or change the commit message.

Also, HEAD~ is equal to HEAD~1

CASE 1.2: Hard Reset Git Commit

If you really don’t care about keeping the changes you have made then you can hard reset your git commit as shown below.

$ git reset --soft HEAD~

02 Git Revert Pushed or Public Commit

Did you already push your commit and want to remove/revert those changed you can follow the below steps. We hope you like it.

2.1 Using git revert

Most of the developers love this approach, and we do too. This is a very straightforward way to remove the commit from the remote Git repository.

Syntax:

$ git revert [hash commit id]

Hash commit id: It is an alphanumeric code that identifies each commit. Use git log command to view the commits.

Example:

Let’s see a very simple example for better understanding.

1. Let’s first get the bad commit ID using git log to use in git revert command

$ git log

commit b212b080791fc34a9f9f5a6c881b651ccf2c59d7
Author: ScratchCode <[email protected]>
Date:   Sun Jun 7 14:05:43 2020 +0530

    Bad commit

commit 43bd27795090e697adc34fe1233fc1c0dc5338d6
Author: ScratchCode <[email protected]>
Date:   Sun Jun 7 11:01:17 2020 +0530

    Initial commit

2. We have two commits in our branch, now let’s copy the bad commit ID and run the git revert as below.

$ git revert b212b080791fc34a9f9f5a6c881b651ccf2c59d7

It will ask you to add a new commit message in your terminal or It might open the editor. So you just need to add your new commit message. In case, if it will open the editor for a new commit message then you need to add your commit message and close the editor tab.

Added new commit message as below at the last line

Revert "Bad commit"

This reverts commit b212b080791fc34a9f9f5a6c881b651ccf2c59d7.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
# Your branch is up to date with 'origin/master'.
#
# Changes to be committed:
#	deleted:    list-view.php
#

New commit message
revert last commit from git
If it’s open new editor then add new commit message shown in this screenshot

After adding commit message you will see something like below

$ git revert b212b080791fc34a9f9f5a6c881b651ccf2c59d7
[master 338ea19] Revert "Bad commit"
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 list-view.php

3. Last but not least push your commit using git push origin <branch name> command.

$ git push origin master

2.2 Using git reset

We have already seen git reset as above to remove the last local commit, So here we will use the same command with --hard flag and push the changes forcefully, and we will be done. Let’s see how we do it.

$ git reset --hard [ previos good commit id ]
$ git commit -m "New message"
$ git push origin [branch name] -f

Example:

As we have already shown above, you need to use git log command to view the commits and just need to copy the previous commit or desired commit ID and run the following commands. Let’s do it step by step.

1. Let’s first get the previous commit ID using git log to use in git reset command as shown below.

$ git log
commit dc4963889120bc406d6b9b02c60906ecdd631494 (HEAD -> list-view, origin/list-view)
Author: ScratchCode <[email protected]>
Date:   Sun Jun 7 15:22:10 2020 +0530

    Changes added

commit 0f712a2685fba022cd1a602ef998da928f9a9555
Author: ScratchCode <[email protected]>
Date:   Sun Jun 7 15:21:28 2020 +0530

    List view added

2. Now, We want to remove the “Changes added” commit from the above list of commits so we need to copy the previous commit ID (0f712a2685fba022cd1a602ef998da928f9a9555) of “List view added”. Let’s copy and run the git reset command.

$ git reset --hard 0f712a2685fba022cd1a602ef998da928f9a9555

3. Then we just need to commit our changes by using git commit -m “message”

$ git commit -m "bla bla changes reverted"

4. Let’s assume we have a "list-view" branch. Now, push the changes forcefully using -f flag to the branch as shown below.

$ git push origin list-view -f

Additionally read our guide, How to Undo add file in Git Before Commit

That’s it from our end. We hope this article helped you to learn how to undo last commit in Git

Please let us know in the comments if everything worked as expected, your issues or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!

 
 

Leave a Comment