âšī¸ 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.1 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://www.freecodecamp.org/news/git-revert-commit-how-to-undo-the-last-commit/ |
| Last Crawled | 2026-04-06 08:00:01 (3 days ago) |
| First Indexed | 2021-08-31 22:33:00 (4 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Git Revert Commit â How to Undo the Last Commit |
| Meta Description | Say you're working on your code in Git and something didn't go as planned. So now you need to revert your last commit. How do you do it? Let's find out! There are two possible ways to undo your last commit. We'll look at both of them in this article.... |
| Meta Canonical | null |
| Boilerpipe Text | Say you're working on your code in Git and something didn't go as planned. So now you need to revert your last commit. How do you do it? Let's find out!
There are two possible ways to undo your last commit. We'll look at both of them in this article.
The
revert
command
The
revert
command will create a commit that reverts the changes of the commit being targeted. You can use it to revert the last commit like this:
git revert <commit to revert>
You can find the name of the commit you want to revert using
[git log](https://www.freecodecamp.org/news/git-log-command/)
. The first commit that's described there is the last commit created. Then you can copy from there the alphanumerical name and use that in the
revert
command.
In this image, each circe represents a commit.
The
reset
command
You can also use the
reset
command to undo your last commit. But be careful â it will change the commit history, so you should use it rarely. It will move the HEAD, the working branch, to the indicated commit, and discard anything after:
git reset --soft HEAD~
1
The
--soft
option means that you will not lose the uncommitted changes you may have.
In this image, each circle represents a commit.
If you want to reset to the last commit and also remove all unstaged changes, you can use the
--hard
option:
git reset --hard HEAD~
1
This will undo the latest commit, but also any uncommitted changes.
In this image, each circle represents a commit.
Should You Use
reset
or
revert
in Git?
You should really only use
reset
if the commit being reset only exists locally. This command changes the commit history and it might overwrite history that remote team members depend on.
revert
instead creates a
new commit
that undoes the changes, so if the commit to revert has already been pushed to a shared repository, it is best to use
revert
as it doesn't overwrite commit history.
Conclusion
You have learned two ways to undo the last commit and also when it's best to use one over the other.
Now if you notice that your last commit introduces a bug or should have not been committed you know how to fix that!
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers.
Get started |
| Markdown | [](https://www.freecodecamp.org/news/)
Menu Menu
- [Forum](https://forum.freecodecamp.org/)
- [Curriculum](https://www.freecodecamp.org/learn)
[Donate](https://www.freecodecamp.org/donate/)
[Learn to code â free 3,000-hour curriculum](https://www.freecodecamp.org/)
August 31, 2021
/ [\#Git](https://www.freecodecamp.org/news/tag/git/)
# Git Revert Commit â How to Undo the Last Commit

[Ilenia Magoni](https://www.freecodecamp.org/news/author/uccellino95/)

Say you're working on your code in Git and something didn't go as planned. So now you need to revert your last commit. How do you do it? Let's find out\!
There are two possible ways to undo your last commit. We'll look at both of them in this article.
## The `revert` command
The `revert` command will create a commit that reverts the changes of the commit being targeted. You can use it to revert the last commit like this:
```
git revert <commit to revert>
```
You can find the name of the commit you want to revert using `[git log](https://www.freecodecamp.org/news/git-log-command/)`. The first commit that's described there is the last commit created. Then you can copy from there the alphanumerical name and use that in the `revert` command.
 *In this image, each circe represents a commit.*
## The `reset` command
You can also use the `reset` command to undo your last commit. But be careful â it will change the commit history, so you should use it rarely. It will move the HEAD, the working branch, to the indicated commit, and discard anything after:
```
git reset --soft HEAD~1
```
The `--soft` option means that you will not lose the uncommitted changes you may have.
 *In this image, each circle represents a commit.*
If you want to reset to the last commit and also remove all unstaged changes, you can use the `--hard` option:
```
git reset --hard HEAD~1
```
This will undo the latest commit, but also any uncommitted changes.
 *In this image, each circle represents a commit.*
## Should You Use `reset` or `revert` in Git?
You should really only use `reset` if the commit being reset only exists locally. This command changes the commit history and it might overwrite history that remote team members depend on.
`revert` instead creates a *new commit* that undoes the changes, so if the commit to revert has already been pushed to a shared repository, it is best to use `revert` as it doesn't overwrite commit history.
# Conclusion
You have learned two ways to undo the last commit and also when it's best to use one over the other.
Now if you notice that your last commit introduces a bug or should have not been committed you know how to fix that\!
***

[Ilenia Magoni](https://www.freecodecamp.org/news/author/uccellino95/)
Read [more posts](https://www.freecodecamp.org/news/author/uccellino95/).
***
If this article was helpful, share it.
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. [Get started](https://www.freecodecamp.org/learn)
ADVERTISEMENT
freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)
Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons - all freely available to the public.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.
You can [make a tax-deductible donation here](https://www.freecodecamp.org/donate/).
## Trending Books and Handbooks
- [REST APIs](https://www.freecodecamp.org/news/build-consume-and-document-a-rest-api/)
- [Clean Code](https://www.freecodecamp.org/news/how-to-write-clean-code/)
- [TypeScript](https://www.freecodecamp.org/news/learn-typescript-with-react-handbook/)
- [JavaScript](https://www.freecodecamp.org/news/learn-javascript-for-beginners/)
- [AI Chatbots](https://www.freecodecamp.org/news/how-to-build-an-ai-chatbot-with-redis-python-and-gpt/)
- [Command Line](https://www.freecodecamp.org/news/command-line-for-beginners/)
- [GraphQL APIs](https://www.freecodecamp.org/news/building-consuming-and-documenting-a-graphql-api/)
- [CSS Transforms](https://www.freecodecamp.org/news/complete-guide-to-css-transform-functions-and-properties/)
- [Access Control](https://www.freecodecamp.org/news/how-to-build-scalable-access-control-for-your-web-app/)
- [REST API Design](https://www.freecodecamp.org/news/rest-api-design-best-practices-build-a-rest-api/)
- [PHP](https://www.freecodecamp.org/news/the-php-handbook/)
- [Java](https://www.freecodecamp.org/news/the-java-handbook/)
- [Linux](https://www.freecodecamp.org/news/learn-linux-for-beginners-book-basic-to-advanced/)
- [React](https://www.freecodecamp.org/news/react-for-beginners-handbook/)
- [CI/CD](https://www.freecodecamp.org/news/learn-continuous-integration-delivery-and-deployment/)
- [Docker](https://www.freecodecamp.org/news/the-docker-handbook/)
- [Golang](https://www.freecodecamp.org/news/learn-golang-handbook/)
- [Python](https://www.freecodecamp.org/news/the-python-handbook/)
- [Node.js](https://www.freecodecamp.org/news/get-started-with-nodejs/)
- [Todo APIs](https://www.freecodecamp.org/news/build-crud-operations-with-dotnet-core-handbook/)
- [JavaScript Classes](https://www.freecodecamp.org/news/how-to-use-classes-in-javascript-handbook/)
- [Front-End Libraries](https://www.freecodecamp.org/news/front-end-javascript-development-react-angular-vue-compared/)
- [Express and Node.js](https://www.freecodecamp.org/news/the-express-handbook/)
- [Python Code Examples](https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/)
- [Clustering in Python](https://www.freecodecamp.org/news/clustering-in-python-a-machine-learning-handbook/)
- [Software Architecture](https://www.freecodecamp.org/news/an-introduction-to-software-architecture-patterns/)
- [Programming Fundamentals](https://www.freecodecamp.org/news/what-is-programming-tutorial-for-beginners/)
- [Coding Career Preparation](https://www.freecodecamp.org/news/learn-to-code-book/)
- [Full-Stack Developer Guide](https://www.freecodecamp.org/news/become-a-full-stack-developer-and-get-a-job/)
- [Python for JavaScript Devs](https://www.freecodecamp.org/news/learn-python-for-javascript-developers-handbook/)
## Mobile App
- [](https://apps.apple.com/us/app/freecodecamp/id6446908151?itsct=apps_box_link&itscg=30200)
- [](https://play.google.com/store/apps/details?id=org.freecodecamp)
## Our Charity
[Publication powered by Hashnode](https://hashnode.com/) [About](https://www.freecodecamp.org/news/about/) [Alumni Network](https://www.linkedin.com/school/free-code-camp/people/) [Open Source](https://github.com/freeCodeCamp/) [Shop](https://www.freecodecamp.org/news/shop/) [Support](https://www.freecodecamp.org/news/support/) [Sponsors](https://www.freecodecamp.org/news/sponsors/) [Academic Honesty](https://www.freecodecamp.org/news/academic-honesty-policy/) [Code of Conduct](https://www.freecodecamp.org/news/code-of-conduct/) [Privacy Policy](https://www.freecodecamp.org/news/privacy-policy/) [Terms of Service](https://www.freecodecamp.org/news/terms-of-service/) [Copyright Policy](https://www.freecodecamp.org/news/copyright-policy/) |
| Readable Markdown | 
Say you're working on your code in Git and something didn't go as planned. So now you need to revert your last commit. How do you do it? Let's find out\!
There are two possible ways to undo your last commit. We'll look at both of them in this article.
## The `revert` command
The `revert` command will create a commit that reverts the changes of the commit being targeted. You can use it to revert the last commit like this:
```
git revert <commit to revert>
```
You can find the name of the commit you want to revert using `[git log](https://www.freecodecamp.org/news/git-log-command/)`. The first commit that's described there is the last commit created. Then you can copy from there the alphanumerical name and use that in the `revert` command.
 *In this image, each circe represents a commit.*
## The `reset` command
You can also use the `reset` command to undo your last commit. But be careful â it will change the commit history, so you should use it rarely. It will move the HEAD, the working branch, to the indicated commit, and discard anything after:
```
git reset --soft HEAD~1
```
The `--soft` option means that you will not lose the uncommitted changes you may have.
 *In this image, each circle represents a commit.*
If you want to reset to the last commit and also remove all unstaged changes, you can use the `--hard` option:
```
git reset --hard HEAD~1
```
This will undo the latest commit, but also any uncommitted changes.
 *In this image, each circle represents a commit.*
## Should You Use `reset` or `revert` in Git?
You should really only use `reset` if the commit being reset only exists locally. This command changes the commit history and it might overwrite history that remote team members depend on.
`revert` instead creates a *new commit* that undoes the changes, so if the commit to revert has already been pushed to a shared repository, it is best to use `revert` as it doesn't overwrite commit history.
## Conclusion
You have learned two ways to undo the last commit and also when it's best to use one over the other.
Now if you notice that your last commit introduces a bug or should have not been committed you know how to fix that\!
***
***
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. [Get started](https://www.freecodecamp.org/learn) |
| Shard | 32 (laksa) |
| Root Hash | 13723046482134587832 |
| Unparsed URL | org,freecodecamp!www,/news/git-revert-commit-how-to-undo-the-last-commit/ s443 |