Undo the most recent local Git commits

David Y.

The Problem

How do I undo the most recent local commits in Git?

The Solution

The git reset command will return the current branch to a specified previous commit. By default, this command will remove commits from the current branch’s history while leaving the files in the working tree untouched. This allows you to redo one or more commits without losing any work.

When calling git reset, you need to specify the commit to reset to. You can get the hash of the commit you want from git log, or you can specify an ancestor of HEAD, the current commit, using the tilde (~) suffix. The following commands will undo and redo the most recent commit:

git add . git commit -m "This commit is a mistake" git reset HEAD~ git add main.py # need to re-add files after reset git commit -m "This commit corrects the mistake"

To undo the last two commits, use the commands:

git add . git commit -m "This commit is a mistake" # make changes git add . git commit -m "This commit is another mistake" git reset HEAD~2 git add . git commit -m "this commit corrects both mistakes"

If you don’t want to have to re-stage your files after a reset, you can use the --soft flag:

git add . git commit -m "This commit is a mistake" git reset --soft HEAD~ # no need to git add, as files are already staged git commit -m "This commit corrects the mistake"

If you want to reset both the Git history and the working tree to the state of a previous commit, you can use the --hard flag. Note that this will reverse all changes made to tracked files, including ones that haven’t yet been committed.

git add . git commit -m "" git reset --hard HEAD~

git reset --hard should be used with caution. However, you can still retrieve any deleted commits using git reflog, up to 90 days after they were deleted. When run, git reflog will show a list of commits previously on the tips of branches. From this list, you can pick out the partial hash of the commit (e.g. 5c8f5a7) to restore and create a new branch for it:

git checkout -b restored-commit-branch 5c8f5a7

If you would like to preserve your repository’s history but return the files to a previous state, you can use git revert to create new commits that do the opposite of existing commits, i.e. removing lines and files that were added and adding lines and files that were removed:

git add . git commit -m "This commit is a mistake" git revert HEAD # will create a new commit doing the opposite of the one above

For more on git revert, see our answer for reverting to a previous commit.

Join the discussionCome work with us
Share on Twitter
Bookmark this page
Ask a questionImprove this Answer

Related Answers

A better experience for your users. An easier life for your developers.

© 2023 • Sentry is a registered Trademark
of Functional Software, Inc.