Sentry Answers>Git>

Undo the most recent local Git commits

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:

Click to Copy
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:

Click to Copy
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:

Click to Copy
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.

Click to Copy
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:

Click to Copy
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:

Click to Copy
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.

Further Reading

If you’re looking to get a deeper understanding of how web performance optimization works, take a look at the following articles:

  • Syntax.fmListen to the Syntax Podcast
  • ResourcesWhat is Distributed Tracing
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

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