ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://sentry.io/answers/undo-the-most-recent-local-git-commits/ |
| Last Crawled | 2026-04-08 03:30:24 (6 hours ago) |
| First Indexed | 2023-06-21 06:47:38 (2 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Undo the most recent local Git commits | Sentry |
| Meta Description | Sentry helps developers monitor and fix crashes in real time. Get the details you need to resolve the most important issues quickly. |
| Meta Canonical | null |
| Boilerpipe Text | 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
.
Further Reading
If you’re looking to get a deeper understanding of how web performance optimization works, take a look at the following articles:
From LCP to CLS: Improve your Core Web Vitals with Image Loading Best Practices
How to identify and fix Render-Blocking Resources
Low effort image optimization tips
5 easy tips to improve your personal website performance
How to hack your Google Lighthouse scores in 2024 |
| Markdown | [Skip to main content](https://sentry.io/answers/undo-the-most-recent-local-git-commits/#main-content)
Menu
Platform
Products
- [Error Monitoring](https://sentry.io/product/error-monitoring/)
- [Logs](https://sentry.io/product/logs/)
- [Session Replay](https://sentry.io/product/session-replay/)
- [Tracing](https://sentry.io/product/tracing/)
- [Seer NEW](https://sentry.io/product/seer/)
- [Profiling](https://sentry.io/product/profiling/)
- [AI Code Review NEW](https://sentry.io/product/seer/ai-code-review/)
- [Size Analysis NEW](https://sentry.io/product/size-analysis)
- [Cron Monitoring](https://sentry.io/product/cron-monitoring/)
- [Uptime Monitoring](https://sentry.io/product/uptime-monitoring/)
- [Sentry CLI NEW](https://cli.sentry.dev/)
Integrations
- [Github](https://sentry.io/integrations/github/)
- [Slack](https://sentry.io/integrations/slack/)
- [Sentry MCP NEW](https://mcp.sentry.dev/)
- [All Integrations](https://sentry.io/integrations/)
SDKs
- [Javascript](https://sentry.io/for/javascript/)
- [Python](https://sentry.io/for/python/)
- [React](https://sentry.io/for/react/)
- [Laravel](https://sentry.io/for/laravel/)
- [Next.js](https://sentry.io/for/nextjs/)
- [All SDKs](https://sentry.io/platforms/)
Solutions
### Solutions
- [Web / Full Stack Development](https://sentry.io/for/full-stack/)
- [Mobile Crash Reporting](https://sentry.io/solutions/mobile-developers/)
- [Game Crash Reporting](https://sentry.io/solutions/game-developers/)
- [AI Observability](https://sentry.io/solutions/ai-observability/)
- [Application Performance Monitoring](https://sentry.io/solutions/application-performance-monitoring/)
- [Real User Monitoring](https://sentry.io/solutions/real-user-monitoring-rum/)
- [Ecommerce](https://sentry.io/solutions/ecommerce/)
- [Enterprise](https://sentry.io/for/enterprise/)
- [Startups](https://sentry.io/for/startups/)
Resources
Learn
- [Blog](https://blog.sentry.io/)
- [Changelog](https://sentry.io/changelog/)
- [Sandbox](https://sandbox.sentry.io/)
- [Resources](https://sentry.io/resources/)
- [Syntax](https://syntax.fm/)
- [Customers](https://sentry.io/customers/)
- [Cookbook](https://sentry.io/cookbook/)
Support
- [Contact Us](https://sentry.io/contact/enterprise/)
- [Help Center](https://help.sentry.io/)
- [Status](https://status.sentry.io/)
Hang out with us
- [Sentry Build](https://sentry.io/resources/sentry-build/)
- [Events](https://sentry.io/events/)
- [Merch](https://sentry.shop/)
[Docs](https://docs.sentry.io/)
[Pricing](https://sentry.io/pricing/)
[Sign In](https://sentry.io/auth/login/) [Get Demo](https://sentry.io/demo/) [Get Started](https://sentry.io/signup/)
- Platform
- Solutions
- [Web / Full Stack Development](https://sentry.io/for/full-stack/)
- [Mobile Crash Reporting](https://sentry.io/solutions/mobile-developers/)
- [Game Crash Reporting](https://sentry.io/solutions/game-developers/)
- [AI Observability](https://sentry.io/solutions/ai-observability/)
- [Application Performance Monitoring](https://sentry.io/solutions/application-performance-monitoring/)
- [Real User Monitoring](https://sentry.io/solutions/real-user-monitoring-rum/)
- [Ecommerce](https://sentry.io/solutions/ecommerce/)
- [Enterprise](https://sentry.io/for/enterprise/)
- [Startups](https://sentry.io/for/startups/)
- Resources
- [Docs](https://docs.sentry.io/)
- [Pricing](https://sentry.io/pricing/)
[Sign In](https://sentry.io/auth/login/) [Get Demo](https://sentry.io/demo/) [Get Started](https://sentry.io/signup/)
[Sentry Answers](https://sentry.io/answers/) \> [Git](https://sentry.io/answers/git/) \> Undo the most recent local Git commits
# Undo the most recent local Git commits

David Y. —
February 15, 2023
[jump to solution](https://sentry.io/answers/undo-the-most-recent-local-git-commits/#the-solution)
## 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:
```
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`](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:
```
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](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 web performance optimization 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/)
### More Git content
- Resources [What is Distributed Tracing (opens in a new tab)](https://sentry.io/resources/distributed-tracing-explained/)
- Community Series [Identify, Trace, and Fix Endpoint Regression Issues (opens in a new tab)](https://sentry.io/community/series/monitor-endpoint-regression/)
-  [Listen to the Syntax Podcast (opens in a new tab)](https://syntax.fm/?utm_source=sentry&utm_medium=display&utm_campaign=syntax-na-podcastpromo&utm_content=logo-answers-rail-banner-listen)
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
[SEE EPISODES](https://syntax.fm/?utm_source=sentry&utm_medium=display&utm_campaign=syntax-na-podcastpromo&utm_content=logo-answers-rail-banner-listen)
## Related Answers
[Delete a commit from a branch in Git](https://sentry.io/answers/delete-a-commit-from-a-branch-in-git/)
David Y. —
June 15, 2023
[Create a new Git branch from an existing branch](https://sentry.io/answers/create-a-new-git-branch-from-an-existing-branch/)
David Y. —
July 15, 2023
[Revert a Git repository to a previous commit](https://sentry.io/answers/revert-a-git-repository-to-a-previous-commit/)
David Y. —
February 15, 2023
[Abort a merge in Git](https://sentry.io/answers/abort-a-merge-in-git/)
David Y. —
July 15, 2023
Considered "not bad" by 4 million developers and more than 150,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.
[learn more about sentry](https://sentry.io/) [join our discord](https://discord.com/invite/sentry)

### Company
- [About](https://sentry.io/about/)
- [Blog](https://blog.sentry.io/)
- [Careers](https://sentry.io/careers/)
- [Contact Us](https://sentry.io/contact/enterprise/)
- [Trust](https://sentry.io/trust/)
### Platform
- [Error Monitoring](https://sentry.io/product/error-monitoring/)
- [Tracing](https://sentry.io/product/tracing/)
- [Session Replay](https://sentry.io/product/session-replay/)
- [Seer](https://sentry.io/product/seer/)
- [Logs](https://sentry.io/product/logs/)
- [Uptime Monitoring](https://sentry.io/product/uptime-monitoring/)
- [Profiling](https://sentry.io/product/profiling/)
- [Cron Monitoring](https://sentry.io/product/cron-monitoring/)
- [Integrations](https://sentry.io/integrations/)
### Solutions
- [Web / Full Stack Development](https://sentry.io/for/full-stack/)
- [Mobile Crash Reporting](https://sentry.io/solutions/mobile-developers/)
- [Game Crash Reporting](https://sentry.io/solutions/game-developers/)
- [AI Observability](https://sentry.io/solutions/ai-observability/)
- [Application Performance Monitoring](https://sentry.io/solutions/application-performance-monitoring/)
- [Real User Monitoring](https://sentry.io/solutions/real-user-monitoring-rum/)
- [Ecommerce](https://sentry.io/solutions/ecommerce/)
- [Enterprise](https://sentry.io/for/enterprise/)
- [Startups](https://sentry.io/for/startups/)
### Get Help
- [Docs](https://docs.sentry.io/)
- [Help Center](https://help.sentry.io/)
- [Status](https://status.sentry.io/)
- [Dev Resources](https://develop.sentry.dev/getting-started/)
[Terms](https://sentry.io/terms/) [Security & Compliance](https://sentry.io/security/) [Privacy](https://sentry.io/privacy/)
[ Twitter Menu Button](https://x.com/sentry/) [ Github Social Menu Button](https://github.com/getsentry/) [ LinkedIn Menu Button](https://linkedin.com/company/getsentry/) [ Discord Menu Button](https://discord.gg/sentry)
© 2026 • Sentry is a registered Trademark of Functional Software, Inc. |
| Readable Markdown | ## 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:
```
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`](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:
```
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](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 web performance optimization 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/) |
| Shard | 147 (laksa) |
| Root Hash | 17800916657557307547 |
| Unparsed URL | io,sentry!/answers/undo-the-most-recent-local-git-commits/ s443 |