# Undo the Most Recent Local Git Commits Safely

> Undo local Git commits using git reset with soft, mixed, or hard flags, with examples for preserving changes and recovering deleted commits via reflog

**URL:** https://sentry.io/answers/undo-the-most-recent-local-git-commits/

---

## The Problem

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

## The Solution

The [`git reset`](https://git-scm.com/docs/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](https://git-scm.com/docs/git-rev-parse#Documentation/git-rev-parse.txt-emltrevgtltngtemegemHEADmaster3em). The following commands will undo and redo the most recent commit:

```sh
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:

```sh
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:

```sh
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.

```sh
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`](https://git-scm.com/docs/git-reflog), [up to 90 days](https://git-scm.com/docs/git-reflog#Documentation/git-reflog.txt---expirelttimegt) 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:

```sh
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:

```sh
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](https://sentry.io/answers/revert-a-git-repository-to-a-previous-commit/).

## Further Reading

If you're looking to get a deeper understanding of how application monitoring works, take a look at the following articles:

- [From LCP to CLS: Improve your Core Web Vitals with Image Loading Best Practices](https://blog.sentry.io/from-lcp-to-cls-improve-your-core-web-vitals-with-image-loading-best/)
- [How to identify and fix Render-Blocking Resources](https://blog.sentry.io/how-to-id-fix-render-blocking-resources/)
- [Low effort image optimization tips](https://blog.sentry.io/low-effort-image-optimization-tips/)
- [5 easy tips to improve your personal website performance](https://blog.sentry.io/5-easy-tips-to-improve-your-personal-website-performance/)
- [How to hack your Google Lighthouse scores in 2024](https://blog.sentry.io/how-to-hack-your-google-lighthouse-scores-in-2024/)

---

*Source: [sentry.io/answers/undo-the-most-recent-local-git-commits/](https://sentry.io/answers/undo-the-most-recent-local-git-commits/)*
