DEV Community

Cover image for How to undo the last commit
Isabel Costa
Isabel Costa

Posted on

Git Revert Pushed Commit How to undo the last commit

In this post I will show how I sometimes recover wrong changes (commits) in a coding project, using git on the command line.

Why would I want to do this?

In my thesis, I’m working on a project that I develop in one environment, and then test in another environment composed of multiple virtual machines. So each important change that I do may have a significant impact on the functionalities of the project. Sometimes, the change I do might not have the result I expected. Then I have to see the changes and analyze the project’s behavior before and after the last commit.

How do you see the last commit?

To test a specific commit, you need the hash. To get the hash you can run git log, then you get this output:

root@debian:/home/debian/test-project# git log
commit <last commit hash>
Author: Isabel Costa <example@email.com>
Date:   Sun Feb 4 21:57:40 2018 +0000

<commit message>

commit <before last commit hash>
Author: Isabel Costa <example@email.com>
Date:   Sun Feb 4 21:42:26 2018 +0000

<commit message>

(...)

You can also run git log --oneline to simplify the output:

root@debian:/home/debian/test-project# git log --oneline
<last commit hash> <commit message>
cdb76bf Added another feature
d425161 Added one feature

(...)

To test a specific commit (e.g.: <before last commit hash>), that you think has the last working version, you can type the following:

git checkout <commit hash>

This will make the working repository match the state of this exact commit. 

After you do this you’ll get the following output:

root@debian:/home/debian/test-project# git checkout <commit hash>
Note: checking out '<commit hash>'.

You are in 'detached HEAD' state. You can look around, make experimental changes
and commit them, and you can discard any commits you make in this state without
impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may do so
(now or later) by using -b with the checkout command again. Example:

git checkout -b new_branch_name

HEAD is now at <commit hash>... <commit message>

After analyzing the specific commit, if you then decide to stay in that commit state, you can undo the last commit.

How to undo this commit?

If you wish to undo/revert the last commit you can do the following, using the commit hash that you get from the git log command:

git revert <commit hash>

This command will create a new commit with the “Revert” word in the beginning of the message. After this, if you check your repository status, you’ll notice that you have the HEAD detached at the commit you tested before.

root@debian:/home/debian/test-project# git status
HEAD detached at 69d885e

(...)

You don’t want to see this message, so to fix this and attach back the HEAD to your working repository, you should checkout the branch you are working on:

git checkout <current branch>

During the making of this post, I found this tutorial — Undoing Commits and Changes —  by Atlassian, which describes very well this issue.

Summary

  • If you want to test the previous commit just do git checkout <test commit hash>; then you can test that last working version of your project.

  • If you want to revert the last commit just do git revert <unwanted commit hash>; then you can push this new commit, which undid your previous commit.

  • To fix the detached head do git checkout <current branch>.


You can find me on Twitter, LinkedIn, Github, Medium and my personal website.

Latest comments (23)

Collapse
 
make_a_calendar profile image
Make a Calendar

Great post! It’s super helpful to have a clear explanation of how to undo a commit, especially in situations where a small mistake can have a big impact on a project. I appreciate the way you break down the steps and provide context for why this can be important in a real development environment. Thanks for sharing this useful guide!

Collapse
 
gbwhoue profile image
gbwhoue

There are two ways to undo the last commit in Git:

  • Using git reset: This command will remove the last commit from your local repository, but it will not undo any changes that you have made to your files. To use git reset, simply run the following command:

Code

git reset HEAD~1
Enter fullscreen mode Exit fullscreen mode

This will reset your HEAD pointer to the previous commit, and the last commit will be removed from your local repository.

  • Using git revert: This command will create a new commit that undoes the changes made in the last commit. To use git revert, simply run the following command:

Code

git revert HEAD~1
Enter fullscreen mode Exit fullscreen mode

This will create a new commit that undoes the changes made in the last commit. The new commit will be added to your local repository, and the last commit will remain in your repository history.

Which method you choose to use depends on your specific needs. If you want to undo the last commit and keep the changes that you have made to your files, then you should use git reset. If you want to undo the last commit and discard the changes that you have made to your files, then you should use git revert.

Here are some additional things to keep in mind when undoing the last commit:

  • You can only undo one commit at a time. If you want to undo multiple commits, you will need to run the git reset or git revert command multiple times.
  • If you have already pushed the last commit to a remote repository, then you will need to push the new commit that you created with git revert to the remote repository as well gbprowa.com.
  • If you have any uncommitted changes in your working directory, then you will lose those changes when you run the git reset or git revert command.

Please be careful when using the git reset and git revert commands, as they can both have unintended consequences if used incorrectly

Collapse
 
pradeepkatiyar007 profile image
Pradeep Katiyar

The last time when I tried to undo the commit I failed, but this guide gives me enough knowledge about it.
Thanks!
MBOX Converter

Collapse
 
wilkinson profile image
wilkinson

This is the safest option if you want to preserve the changes you made but simply need to fix the commit message or add more changes before committing again.

Use the following command:

git reset --soft HEAD~
This command:

Uses git reset to modify the HEAD, which points to the latest commit.
The --soft flag tells Git to keep the changes made in the last commit as unstaged changes in your working directory.
HEAD~ refers to the parent commit of the current HEAD, effectively undoing the last commit.
Join the online community of geometry dash lite! Millions of players share their creations, challenges, and experiences, fostering a passionate atmosphere.

Collapse
 
alexwrinner profile image
alexwrinner • Edited

Works great! 👍😃

Collapse
 
opentechconsult profile image
OpenTech-Consult

Have you considered the fact that the code is pushed to a remote repository like Github. I am sorry but the explanations are not very clear for me.

Collapse
 
nelsonblack profile image
Nelson Bwogora • Edited

visual studio git tools you can select undo last commit

Collapse
 
reyz_mynick profile image
Roes W

If it's in feature branch I just do hard reset and force push. Revert commit is best solution to revert merge pull request in main/master branch.

Collapse
 
wiltel492019 profile image
Wiltel492019

Great information that you presented.Commits and changes.Great reach out on your message.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.