DEV Community

Cover image for How to undo the last commit

How to undo the last commit

Isabel Costa on February 20, 2018

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 ...
Collapse
 
ghost profile image
Ghost

I do undo differently, in my ~/.gitconfig I have this alias:

[alias]
    undo = reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode

Works great!

Collapse
 
isabelcmdcosta profile image
Isabel Costa

Awesome :) more options! I did not know about alias on git.

Collapse
 
lt0mm profile image
Tom • Edited

also a really powerful command is interactive rebase, though also it changes history for example

git rebase -i HEAD~3
Enter fullscreen mode Exit fullscreen mode

it means you want to make changes in last 3 commits, you can squash them, delete them, change messages, by the way I use it a lot in my feature branches even when I already pushed some commits then I do forced push to rewrite history on the server, it is ok till I know that only I work on that branch

Collapse
 
dfockler profile image
Dan Fockler

If you just have a local commit you can also use the git reflog indexes to move back to a point in history even moving back from doing merges and pulls. Something like git reset --hard HEAD@{<reflog index number>}

Collapse
 
jvarness profile image
Jake Varness

Reflog is life

Collapse
 
andrewcarsten25 profile image
Andrew Carsten

Reflog is the life!! tubidy

Collapse
 
saschadev profile image
Der Sascha

For a quick revert you can use git reset HEAD^1 --hard and the you can commit it with additional changes. I thing this "prevents" the revert prefix in the git history. What do you think about it?

Collapse
 
pshchelo profile image
Pavlo Shchelokovskyy

As long as the commit you are 'undoing' is local and is not part of any remote branch (you have not push-ed it yet), or if this is a one-dev pet project and you are sure nobody is 'consuming' your remote branch (and you can push to it with --force) - git reset is OK.

The thing is - it rewrites history. So if you'll make another commit on top of it but the 'undone' commit is pushed already, the history of your local branch and your remote tracking branch will diverge - and you'd have to push it either to another branch, or with --force, rewriting the remote branch. And if the remote branch is rewritten, anyone who had cloned it won't be able to pull from it any more that easily etc...

Thus the general advise is not to rewrite history of remote branches, and that's what git revert is for.

Funny side note - once seen a commit starting with a word Revert repeated 6 times o_O

Collapse
 
isabelcmdcosta profile image
Isabel Costa

Great explanation! In my case, I thought revert was better because I pushed the previous changes before.

Collapse
 
isabelcmdcosta profile image
Isabel Costa

Thanks for the suggestion. I used that before, but in the last problems where I had to undo the commits and then push the changes, that was the solution that worked for me. Also, I didn't mind having a commit dedicated to the revert (with the prefix) on the git history. I think it is a possible solution too.

Collapse
 
arandilopez profile image
Arandi López • Edited

You can undo the latest commit with git reset --soft HEAD^ too. But if you already pushed your changes a revert could be a better option.

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
 
johand profile image
Johan • Edited

I use git reset HEAD~1 to undo the last commit from a local branch and keep the modifications, this is a way for easily fix the changes

Collapse
 
wiltel492019 profile image
Wiltel492019

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

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
 
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
 
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
 
directfilin profile image
FilinVadim

That is bad. Do not do this.

Collapse
 
chrisvasqm profile image
Christian Vasquez • Edited

Could you explain why?

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
 
nelsonblack profile image
Nelson Bwogora • Edited

visual studio git tools you can select undo last commit

Collapse
 
pradeepkatiyar007 profile image
pradeepkatiyar007

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
 
alexwrinner profile image
alexwrinner • Edited

Works great! 👍😃