âšī¸ 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://www.freecodecamp.org/news/git-reverting-to-previous-commit-how-to-revert-to-last-commit/ |
| Last Crawled | 2026-04-06 18:06:21 (3 hours ago) |
| First Indexed | 2022-10-20 01:45:56 (3 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Git Reverting to Previous Commit â How to Revert to Last Commit |
| Meta Description | Git is a great tool for version control. It also makes collaborating with others more efficient. In this article, you'll learn how to revert to previous commits when tracking your project with Git. The two commands we'll discuss in this article are g... |
| Meta Canonical | null |
| Boilerpipe Text | Git
is a great tool for version control. It also makes collaborating with others more efficient.
In this article, you'll learn how to revert to previous commits when tracking your project with Git.
The two commands we'll discuss in this article are
git reset
and
git revert
. These commands can help you undo your commits and go back to a previous commit.
They are not exactly the same, though, so we'll make this article a bit more practical by demonstrating how each command works in a project.
Anyone can follow along with this tutorial because it's not going to be language specific â we'll make use of a text (txt) file.
How to Revert to a Previous Commit Using the
git reset
Command
In this section, we'll go through the process of creating a new file and making three commits. You'll then see how you can revert to either the last commit or any other commit using the commit ID.
To get started, I've created a file called
tasks.txt
. The file has this in it:
1. code.
2. Practice.
3. Build.
Next, we're going to initialize, add, and commit this file:
git
init
git
add
tasks.txt
git
commit
-m
"first commit"
We have made the first commit.
We'll repeat the process above two more times but we'll add an extra line of text to the file before each commit. That is:
1. code.
2. Practice.
3. Build.
4. Research.
git
add
tasks.txt
git
commit
-m
"second commit"
Lastly, for the third commit:
1. code.
2. Practice.
3. Build.
4. Research.
5. Write.
git
add
tasks.txt
git
commit
-m
"third commit"
Now we have three commits. To revert to a previous commit, you must first get the commit ID. To do that, run the command below:
git
log
--oneline
In my terminal, I have this:
git log --oneline
As you can see above, this command lists all your commits along with their IDs.
To go back to the second commit, you run the
git reset
command followed by the commit ID. That is:
git
reset 5914db0
If you've followed up to this point, you'll not notice any difference in the file (you'll see how to undo both the commit and any changes made to the file later).
The file still looks this way:
1. code.
2. Practice.
3. Build.
4. Research.
5. Write.
But when we run the
git log --oneline
command, the third commit wont't be in the log of commits:
git log --oneline
We've successfully gone back to a previous commit.
If you want to undo a commit and the all the changes made after that commit, you attach the
--hard
flag to your
git reset
command.
Let's test this out by reverting back to the first commit:
git
reset 89f6c3d
--hard
This is what the text file looks like now:
1. code.
2. Practice.
3. Build.
We're back to the initial state of the file at the point of the specified commit. All changes that were made to the file after that commit were deleted. When we check the commit log, we'll have just the first commit.
While this seems like something cool to do, you should be careful when using this command. Especially when you're working with a team.
If you undo a commit and delete every file change that came after it, you might lose important changes made to your code by you and other teammates. This will also change the commit history of your project.
Luckily for us, there is way to recover the state of a deleted commit. You can learn more about that
here
.
How to Revert to a Previous Commit Using the
git revert
Command
I have already initialized the project and made three commits like we did in the last section. Here's what the commit log looks like:
git log --oneline
To revert to the to the previous commit, run the
git revert
command along with the commit ID of the current commit.
In our case, we'll be using the ID of the third commit:
git
revert 882ad02
The command above will undo the current commit and revert the file to the state of the previous commit. When you check the commit logs, you'll have something like this:
git log --oneline
Unlike the
git reset
command, the
git revert
command creates a new commit for the reverted changes. The commit where we reverted from will not be deleted.
So as you can see,
git reset
and
git revert
are not the same.
git reset
will undo changes up to the state of the specified commit ID. For example, reverting to the second commit ID will undo changes and leave the state of the file as the state of the second commit.
git revert
will undo changes up to the state before the specified commit ID. For example, reverting to the second commit ID will undo changes and leave the state of the file as the state of the commit that comes before the second commit â the first commit.
The explanations above may seem confusing. The best way to understand it is to try it out yourself.
When to Use
git reset
and
git revert
You should use
git reset
when working on a local repository with changes yet to be pushed remotely. This is because running this command after pulling changes from the remote repo will alter the commit history of the project, leading to merge conflicts for everyone working on the project.
git reset
is a good option when you realize that the changes being made to a particular local branch should be somewhere else. You can reset and move to the desired branch without losing your file changes.
git revert
is a good option for reverting changes pushed to a remote repository. Since this command creates a new commit, you can safely get rid of your mistakes without rearranging the commit history for everyone else.
Summary
In this article, we talked about reverting to previous commits in Git.
We talked about two main commands that showed how to undo Git changes â the
git reset
and
git revert
commands.
We also saw how both commands work using practical examples.
Happy coding!
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/)
October 19, 2022
/ [\#Git](https://www.freecodecamp.org/news/tag/git/)
# Git Reverting to Previous Commit â How to Revert to Last Commit

[Ihechikara Abba](https://www.freecodecamp.org/news/author/Ihechikara/)

[Git](https://www.freecodecamp.org/news/git-and-github-for-beginners/) is a great tool for version control. It also makes collaborating with others more efficient.
In this article, you'll learn how to revert to previous commits when tracking your project with Git.
The two commands we'll discuss in this article are `git reset` and `git revert`. These commands can help you undo your commits and go back to a previous commit.
They are not exactly the same, though, so we'll make this article a bit more practical by demonstrating how each command works in a project.
Anyone can follow along with this tutorial because it's not going to be language specific â we'll make use of a text (txt) file.
## How to Revert to a Previous Commit Using the `git reset` Command
In this section, we'll go through the process of creating a new file and making three commits. You'll then see how you can revert to either the last commit or any other commit using the commit ID.
To get started, I've created a file called `tasks.txt`. The file has this in it:
```
1. code.
2. Practice.
3. Build.
```
Next, we're going to initialize, add, and commit this file:
```
git init
git add tasks.txt
git commit -m "first commit"
```
We have made the first commit.
We'll repeat the process above two more times but we'll add an extra line of text to the file before each commit. That is:
```
1. code.
2. Practice.
3. Build.
4. Research.
```
```
git add tasks.txt
git commit -m "second commit"
```
Lastly, for the third commit:
```
1. code.
2. Practice.
3. Build.
4. Research.
5. Write.
```
```
git add tasks.txt
git commit -m "third commit"
```
Now we have three commits. To revert to a previous commit, you must first get the commit ID. To do that, run the command below:
```
git log --oneline
```
In my terminal, I have this:
 *git log --oneline*
As you can see above, this command lists all your commits along with their IDs.
To go back to the second commit, you run the `git reset` command followed by the commit ID. That is:
```
git reset 5914db0
```
If you've followed up to this point, you'll not notice any difference in the file (you'll see how to undo both the commit and any changes made to the file later).
The file still looks this way:
```
1. code.
2. Practice.
3. Build.
4. Research.
5. Write.
```
But when we run the `git log --oneline` command, the third commit wont't be in the log of commits:
 *git log --oneline*
We've successfully gone back to a previous commit.
If you want to undo a commit and the all the changes made after that commit, you attach the `--hard` flag to your `git reset` command.
Let's test this out by reverting back to the first commit:
```
git reset 89f6c3d --hard
```
This is what the text file looks like now:
```
1. code.
2. Practice.
3. Build.
```
We're back to the initial state of the file at the point of the specified commit. All changes that were made to the file after that commit were deleted. When we check the commit log, we'll have just the first commit.
While this seems like something cool to do, you should be careful when using this command. Especially when you're working with a team.
If you undo a commit and delete every file change that came after it, you might lose important changes made to your code by you and other teammates. This will also change the commit history of your project.
Luckily for us, there is way to recover the state of a deleted commit. You can learn more about that [here](https://www.freecodecamp.org/news/how-to-recover-a-deleted-file-in-git/).
## How to Revert to a Previous Commit Using the `git revert` Command
I have already initialized the project and made three commits like we did in the last section. Here's what the commit log looks like:
 *git log --oneline*
To revert to the to the previous commit, run the `git revert` command along with the commit ID of the current commit.
In our case, we'll be using the ID of the third commit:
```
git revert 882ad02
```
The command above will undo the current commit and revert the file to the state of the previous commit. When you check the commit logs, you'll have something like this:
 *git log --oneline*
Unlike the `git reset` command, the `git revert` command creates a new commit for the reverted changes. The commit where we reverted from will not be deleted.
So as you can see, `git reset` and `git revert` are not the same.
`git reset` will undo changes up to the state of the specified commit ID. For example, reverting to the second commit ID will undo changes and leave the state of the file as the state of the second commit.
`git revert` will undo changes up to the state before the specified commit ID. For example, reverting to the second commit ID will undo changes and leave the state of the file as the state of the commit that comes before the second commit â the first commit.
The explanations above may seem confusing. The best way to understand it is to try it out yourself.
## When to Use `git reset` and `git revert`
You should use `git reset` when working on a local repository with changes yet to be pushed remotely. This is because running this command after pulling changes from the remote repo will alter the commit history of the project, leading to merge conflicts for everyone working on the project.
`git reset` is a good option when you realize that the changes being made to a particular local branch should be somewhere else. You can reset and move to the desired branch without losing your file changes.
`git revert` is a good option for reverting changes pushed to a remote repository. Since this command creates a new commit, you can safely get rid of your mistakes without rearranging the commit history for everyone else.
## Summary
In this article, we talked about reverting to previous commits in Git.
We talked about two main commands that showed how to undo Git changes â the `git reset` and `git revert` commands.
We also saw how both commands work using practical examples.
Happy coding\!
***

[Ihechikara Abba](https://www.freecodecamp.org/news/author/Ihechikara/)
ihechikara.com
***
If you read this far, thank the author to show them you care. Say Thanks
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 | 
[Git](https://www.freecodecamp.org/news/git-and-github-for-beginners/) is a great tool for version control. It also makes collaborating with others more efficient.
In this article, you'll learn how to revert to previous commits when tracking your project with Git.
The two commands we'll discuss in this article are `git reset` and `git revert`. These commands can help you undo your commits and go back to a previous commit.
They are not exactly the same, though, so we'll make this article a bit more practical by demonstrating how each command works in a project.
Anyone can follow along with this tutorial because it's not going to be language specific â we'll make use of a text (txt) file.
## How to Revert to a Previous Commit Using the `git reset` Command
In this section, we'll go through the process of creating a new file and making three commits. You'll then see how you can revert to either the last commit or any other commit using the commit ID.
To get started, I've created a file called `tasks.txt`. The file has this in it:
```
1. code.
2. Practice.
3. Build.
```
Next, we're going to initialize, add, and commit this file:
```
git init
git add tasks.txt
git commit -m "first commit"
```
We have made the first commit.
We'll repeat the process above two more times but we'll add an extra line of text to the file before each commit. That is:
```
1. code.
2. Practice.
3. Build.
4. Research.
```
```
git add tasks.txt
git commit -m "second commit"
```
Lastly, for the third commit:
```
1. code.
2. Practice.
3. Build.
4. Research.
5. Write.
```
```
git add tasks.txt
git commit -m "third commit"
```
Now we have three commits. To revert to a previous commit, you must first get the commit ID. To do that, run the command below:
```
git log --oneline
```
In my terminal, I have this:
 *git log --oneline*
As you can see above, this command lists all your commits along with their IDs.
To go back to the second commit, you run the `git reset` command followed by the commit ID. That is:
```
git reset 5914db0
```
If you've followed up to this point, you'll not notice any difference in the file (you'll see how to undo both the commit and any changes made to the file later).
The file still looks this way:
```
1. code.
2. Practice.
3. Build.
4. Research.
5. Write.
```
But when we run the `git log --oneline` command, the third commit wont't be in the log of commits:
 *git log --oneline*
We've successfully gone back to a previous commit.
If you want to undo a commit and the all the changes made after that commit, you attach the `--hard` flag to your `git reset` command.
Let's test this out by reverting back to the first commit:
```
git reset 89f6c3d --hard
```
This is what the text file looks like now:
```
1. code.
2. Practice.
3. Build.
```
We're back to the initial state of the file at the point of the specified commit. All changes that were made to the file after that commit were deleted. When we check the commit log, we'll have just the first commit.
While this seems like something cool to do, you should be careful when using this command. Especially when you're working with a team.
If you undo a commit and delete every file change that came after it, you might lose important changes made to your code by you and other teammates. This will also change the commit history of your project.
Luckily for us, there is way to recover the state of a deleted commit. You can learn more about that [here](https://www.freecodecamp.org/news/how-to-recover-a-deleted-file-in-git/).
## How to Revert to a Previous Commit Using the `git revert` Command
I have already initialized the project and made three commits like we did in the last section. Here's what the commit log looks like:
 *git log --oneline*
To revert to the to the previous commit, run the `git revert` command along with the commit ID of the current commit.
In our case, we'll be using the ID of the third commit:
```
git revert 882ad02
```
The command above will undo the current commit and revert the file to the state of the previous commit. When you check the commit logs, you'll have something like this:
 *git log --oneline*
Unlike the `git reset` command, the `git revert` command creates a new commit for the reverted changes. The commit where we reverted from will not be deleted.
So as you can see, `git reset` and `git revert` are not the same.
`git reset` will undo changes up to the state of the specified commit ID. For example, reverting to the second commit ID will undo changes and leave the state of the file as the state of the second commit.
`git revert` will undo changes up to the state before the specified commit ID. For example, reverting to the second commit ID will undo changes and leave the state of the file as the state of the commit that comes before the second commit â the first commit.
The explanations above may seem confusing. The best way to understand it is to try it out yourself.
## When to Use `git reset` and `git revert`
You should use `git reset` when working on a local repository with changes yet to be pushed remotely. This is because running this command after pulling changes from the remote repo will alter the commit history of the project, leading to merge conflicts for everyone working on the project.
`git reset` is a good option when you realize that the changes being made to a particular local branch should be somewhere else. You can reset and move to the desired branch without losing your file changes.
`git revert` is a good option for reverting changes pushed to a remote repository. Since this command creates a new commit, you can safely get rid of your mistakes without rearranging the commit history for everyone else.
## Summary
In this article, we talked about reverting to previous commits in Git.
We talked about two main commands that showed how to undo Git changes â the `git reset` and `git revert` commands.
We also saw how both commands work using practical examples.
Happy coding\!
***
***
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-reverting-to-previous-commit-how-to-revert-to-last-commit/ s443 |