ℹ️ 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.datacamp.com/tutorial/git-reset-revert-tutorial |
| Last Crawled | 2026-04-04 06:00:39 (2 days ago) |
| First Indexed | 2022-12-16 14:01:59 (3 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Git Reset and Revert Tutorial for Beginners | DataCamp |
| Meta Description | Learn Git reset and revert to manage your project history with examples using soft, mixed, and hard resets. Learn the difference between Git reset and revert. |
| Meta Canonical | null |
| Boilerpipe Text | What is Git?
Git is an open-source version control tool created in 2005 by Linus Torvalds. It is mainly used to efficiently track changes in project files so that project members can have a record of all the changes and merge the changes without losing valuable information. Our
introductory course to Git
will give you more insights into how and why to use Git while covering the most common steps in Git workflow.
In addition to that, the
Github Concepts
course will provide you with the keys to using Github’s various features, navigating the interface, and successfully performing everyday collaborative tasks.
Overview of Git Revert and Reset
Git revert and Git reset are two commands used in Git to undo changes to a project code and history, but in different ways. In this section, we will provide an overview of how to use both revert and reset. Before diving into the process, let’s create a Git project.
Create a Git project
Below are the instructions to create a git project and initialize it with some files:
mkdir git_revert_reset
cd git_revert_reset
git init .
Was this AI assistant helpful?
mkdir git_revert_reset
creates a folder called
git_revert_reset
.
cd git_revert_reset
moves to the folder.
git init .
initializes the folder as a git repository.
Considering this as a data science project, we can create the following files for data acquisition, data preprocessing, and model training using this
touch
command:
touch data_acquisition.py data_preprocessing.py model_training.py
Was this AI assistant helpful?
This is the final content of the project folder.
git_revert_reset
|---------- data_acquisition.py
|---------- data_preprocessing.py
|---------- model_training.py
Was this AI assistant helpful?
Git Reset
Git Reset diagram (
Source
)
The
git reset
command is used to undo the changes in your working directory and get back to a specific commit while discarding all the commits made after that one.
For instance, imagine you made ten commits. Using
git reset
on the first commit will remove all nine commits, taking you back to the first commit stage.
Before using
git reset
, it is important to consider the type of changes you plan to make; otherwise, you will create more chaos than good.
You can use multiple options along with
git reset
, but these are the main ones. Each one is used depending on a specific situation:
git reset --soft
,
git reset --mixed
, and
git reset --hard
git reset --soft
The
--soft
aims to change the
HEAD
(where the last commit is in your local machine) reference to a specific commit. For instance, if we realize that we forgot to add a file to the commit, we can move back using the
--soft
with respect to the following format:
git reset --soft HEAD~n
to move back to the commit with a specific reference (n).
git reset --soft HEAD~1
gets back to the last commit.
git reset --soft <commit ID>
moves back to the head with the
<commit ID>
.
Let’s look at some examples.
git add data_acquisition.py data_preprocessing.py
git add data
git commit -m
"added data acquisition and preprocessing scripts"
Was this AI assistant helpful?
This is the result of the previous command.
[master (root-commit) faf864e] added python scripts and data folder
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 data_acquisition.py
create mode 100644 data_preprocessing.py
Was this AI assistant helpful?
We can check the status of the commit as follows:
git status
On branch master
Untracked files:
(use
"git add <file>..."
to include in what will be committed)
model_training.py
nothing added to commit but untracked files present (use
"git add"
to track)
Was this AI assistant helpful?
But, we forgot to add the
model_training.py
script to the commit.
Since the last commit is located at the
HEAD
, we can fix this issue by running the following three statements.
Get back to the pre-commit phase using
git reset --soft HEAD
allowing git reset file.
Add the forgotten file with
git add
.
Make the changes with a final commit
git commit
.
git reset --soft HEAD
git add model_training.py
git commit -m
"added all the python scripts"
Was this AI assistant helpful?
The following result made
git reset
to master after all the previous steps:
[master (root-commit) faf864e] added all the python scripts
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 data_acquisition.py
create mode 100644 data_preprocessing.py
create mode 100644 model_training.py
Was this AI assistant helpful?
git reset --mixed
This is the default argument for
git reset
. Running this command has two impacts: (1) uncommit all the changes and (2) unstage them. Imagine that we accidentally added the
model_training.py
file, and we want to remove it because the model training is not finished yet. Here is how to proceed:
Unstage the files that were in the commit with git reset HEAD
Only add the files we need for the commit
git reset HEAD
git status
On branch master
Untracked files:
(use
"git add <file>..."
to include in what will be committed)
model_training.py
data_acquisition.py
data_preprocessing.py
nothing added to commit but untracked files present (use
"git add"
to track)
Was this AI assistant helpful?
Now we can add only the last two files to be committed and make the commit.
git add data_acquisition.py data_preprocessing.py
git commit -m
"Removed the model_training.py from the commit"
Was this AI assistant helpful?
git reset --hard
This option has the potential of being dangerous. So, be cautious when using it!
Basically, when using the hard reset on a specific commit, it forces the HEAD to get back to that commit and deletes everything else after that.
Before running the hard reset, let's have a look at the content of the folder.
ls
data_acquisition.py data_preprocessing.py model_training.py
git reset --hard
HEAD is now at 97159bc added data acquisition and preprocessing scripts
Was this AI assistant helpful?
Now let's check the content of the folder:
ls
data_acquisition.py data_preprocessing.py
Was this AI assistant helpful?
We notice that the untracked file
model_training.py
is deleted. Again, make sure to understand what you are doing with the reset statement because the consequence can be dramatic!
Git Revert
Before and After Revert (
Source
)
Git revert
is similar to
git reset
, but the approach is slightly different. Instead of removing all the commits in its way, the revert ONLY undoes a single commit by taking you back to the staged files before the commit.
So, instead of removing a commit,
git revert
inverts the changes introduced by the original commit by creating a new commit with the underlying inverse content. This is a safe way to revoke a commit because it prevents you from losing your history.
It is important to know that the revert will have no effect unless the commit hash or reference is specified.
Let’s explore a few common git revert options.
git revert --no-edit <commit ID>
The
--no-edit
option allows the user to not change the message used for the commit you attend to revert, and this will make git revert file to master branch.
To illustrate this scenario, let’s create two new files to be added.
touch model_monitoring.py data_monitoring.py
Was this AI assistant helpful?
After creating those files, we need to add them to the modified version staging area.
git add model_monitoring.py
Was this AI assistant helpful?
Before moving on, let’s have a look at the status by running the following command.
git status
Changes to be committed:
(use
"git rm --cached <file>..."
to unstage)
new file: data_monitoring.py
new file: model_monitoring.py
Was this AI assistant helpful?
We can observe that the two files have been added, now, we can commit the changes.
git commit -m
"Added the monitoring script files"
[master (root-commit) eae84e7] Added the monitoring script files
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 data_monitoring.py
create mode 100644 model_monitoring.py
Was this AI assistant helpful?
Then, the commit hash can be generated through the following command. This is beneficial when trying to perform an action to this specific commit.
git reflog
eae84e7 (HEAD -> master) HEAD@{0}: commit (initial): Added the monitoring script files
Was this AI assistant helpful?
Now, let's imagine that the previous commit was not intentional, and we want to get rid of it.
git revert --no-edit eae84e7
[master c61ef6b] Revert
"Added the monitoring script files"
Date: Sat Nov 12 20:14:17 2022 -0600
2 files changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 data_monitoring.py
delete mode 100644 model_monitoring.py
Was this AI assistant helpful?
Let’s have a look at the log of the commits:
git log
commit c61ef6b4e86f41f47c8c77de3b5fca3945a7c075 (HEAD -> master)
Author: Zoumana Keita <keitnekozou@gmail.com>
Date: Sat Nov 12 20:14:17 2022 -0600
Revert
"Added the monitoring script files"
This reverts commit eae84e7669af733ee6c1b854f2fcd9acfea9d4a3.
commit eae84e7669af733ee6c1b854f2fcd9acfea9d4a3
Author: Zoumana Keita <keitnekozou@gmail.com>
Date: Sat Nov 12 20:05:57 2022 -0600
Added the monitoring script files
Was this AI assistant helpful?
From the log, we can notice that a new commit has been created with the ID
c61ef6b4e86f41f47c8c77de3b5fca3945a7c075
to reverse the original commit with the ID
eae84e7669af733ee6c1b854f2fcd9acfea9d4a3
.
In this example, since we have a single commit, we could simply use
git revert HEAD,
which would have the same effect by reverting the last commit.
git revert <commit hash>
This will only remove the changes associated with this commit hash and does not impact any other commit.
Which git revert do you think will have an effect, considering the previously generated ID?
If your answer was
c61ef6b4e86f41f47c8c77de3b5fca3945a7c075
then you are right. Considering
eae84e7669af733ee6c1b854f2fcd9acfea9d4a3
will have no effect, since it is replaced by a new one.
Let’s see what happens when trying to perform the revert.
git revert c61ef6b4e86f41f47c8c77de3b5fca3945a7c075
[master aaead40] Revert
"Revert "
Added the monitoring script files
""
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 data_monitoring.py
create mode 100644 model_monitoring.py
Was this AI assistant helpful?
As you can see, the two files initially created and committed have been brought back!
git revert --no-commit <commit ID>
The purpose of using the
--no-commit
or
-n
option is to prevent git from automatically committing the revert. This can be beneficial if you want to review the changes made by the revert statement before committing them.
Let’s consider the following example:
git revert --no-commit c61ef6b4e86f41f47c8c77de3b5fca3945a7c075
Was this AI assistant helpful?
This will revert the changes made by the commit with the ID
c61ef6b4e86f41f47c8c77de3b5fca3945a7c075
but will not automatically create a new commit to record the revert. Instead, it will leave the changes unstaged so you can review them and decide whether to commit the revert manually.
Git Revert vs. Reset
Git Revert
Using
git revert
, you have the ability to go back to previous states while creating a new commit.
Go for this option if you want to undo changes on a public branch for safety.
Git Reset
With
git reset
, you can go back to the previous commits but you can’t create a new commit.
This option is better when undoing changes on a private branch.
Conclusion
In this tutorial, we have seen how to perform
git reset
and
git revert
changes in your git repository. Remember that a hard reset can be very dangerous because it will cause you to lose your untracked files in the project.
So, what is next? DataCano has multiple tutorials that could be a good next step to improve your Git skills, such as:
How to Resolve Merge Conflicts in Git Tutorial
GitHub, and Git Tutorial for Beginners
Git Push and Pull Tutorial |
| Markdown | [ **Master the world’s most popular programming language.** **All levels welcome.** Register for Free](https://events.datacamp.com/ai-powered-python)
[Skip to main content](https://www.datacamp.com/tutorial/git-reset-revert-tutorial#main)
EN
[English](https://www.datacamp.com/tutorial/git-reset-revert-tutorial)[Español](https://www.datacamp.com/es/tutorial/git-reset-revert-tutorial)[Português](https://www.datacamp.com/pt/tutorial/git-reset-revert-tutorial)[DeutschBeta](https://www.datacamp.com/de/tutorial/git-reset-revert-tutorial)[FrançaisBeta](https://www.datacamp.com/fr/tutorial/git-reset-revert-tutorial)[ItalianoBeta](https://www.datacamp.com/it/tutorial/git-reset-revert-tutorial)[TürkçeBeta](https://www.datacamp.com/tr/tutorial/git-reset-revert-tutorial)[Bahasa IndonesiaBeta](https://www.datacamp.com/id/tutorial/git-reset-revert-tutorial)[Tiếng ViệtBeta](https://www.datacamp.com/vi/tutorial/git-reset-revert-tutorial)[NederlandsBeta](https://www.datacamp.com/nl/tutorial/git-reset-revert-tutorial)[हिन्दीBeta](https://www.datacamp.com/hi/tutorial/git-reset-revert-tutorial)[日本語Beta](https://www.datacamp.com/ja/tutorial/git-reset-revert-tutorial)[한국어Beta](https://www.datacamp.com/ko/tutorial/git-reset-revert-tutorial)[PolskiBeta](https://www.datacamp.com/pl/tutorial/git-reset-revert-tutorial)[RomânăBeta](https://www.datacamp.com/ro/tutorial/git-reset-revert-tutorial)[РусскийBeta](https://www.datacamp.com/ru/tutorial/git-reset-revert-tutorial)[SvenskaBeta](https://www.datacamp.com/sv/tutorial/git-reset-revert-tutorial)[ไทยBeta](https://www.datacamp.com/th/tutorial/git-reset-revert-tutorial)[中文(简体)Beta](https://www.datacamp.com/zh/tutorial/git-reset-revert-tutorial)
***
[More Information](https://support.datacamp.com/hc/en-us/articles/21821832799255-Languages-Available-on-DataCamp)
[Found an Error?]()
[Log in](https://www.datacamp.com/users/sign_in?redirect=%2Ftutorial%2Fgit-reset-revert-tutorial)[Get Started](https://www.datacamp.com/users/sign_up?redirect=%2Ftutorial%2Fgit-reset-revert-tutorial)
Tutorials
[Blogs](https://www.datacamp.com/blog)
[Tutorials](https://www.datacamp.com/tutorial)
[docs](https://www.datacamp.com/doc)
[Podcasts](https://www.datacamp.com/podcast)
[Cheat Sheets](https://www.datacamp.com/cheat-sheet)
[code-alongs](https://www.datacamp.com/code-along)
[Newsletter](https://dcthemedian.substack.com/)
Category
Category
Technologies
Discover content by tools and technology
[AI Agents](https://www.datacamp.com/tutorial/category/ai-agents)[AI News](https://www.datacamp.com/tutorial/category/ai-news)[Artificial Intelligence](https://www.datacamp.com/tutorial/category/ai)[AWS](https://www.datacamp.com/tutorial/category/aws)[Azure](https://www.datacamp.com/tutorial/category/microsoft-azure)[Business Intelligence](https://www.datacamp.com/tutorial/category/learn-business-intelligence)[ChatGPT](https://www.datacamp.com/tutorial/category/chatgpt)[Databricks](https://www.datacamp.com/tutorial/category/databricks)[dbt](https://www.datacamp.com/tutorial/category/dbt)[Docker](https://www.datacamp.com/tutorial/category/docker)[Excel](https://www.datacamp.com/tutorial/category/excel)[Generative AI](https://www.datacamp.com/tutorial/category/generative-ai)[Git](https://www.datacamp.com/tutorial/category/git)[Google Cloud Platform](https://www.datacamp.com/tutorial/category/google-cloud-platform)[Hugging Face](https://www.datacamp.com/tutorial/category/Hugging-Face)[Java](https://www.datacamp.com/tutorial/category/java)[Julia](https://www.datacamp.com/tutorial/category/julia)[Kafka](https://www.datacamp.com/tutorial/category/apache-kafka)[Kubernetes](https://www.datacamp.com/tutorial/category/kubernetes)[Large Language Models](https://www.datacamp.com/tutorial/category/large-language-models)[MongoDB](https://www.datacamp.com/tutorial/category/mongodb)[MySQL](https://www.datacamp.com/tutorial/category/mysql)[NoSQL](https://www.datacamp.com/tutorial/category/nosql)[OpenAI](https://www.datacamp.com/tutorial/category/OpenAI)[PostgreSQL](https://www.datacamp.com/tutorial/category/postgresql)[Power BI](https://www.datacamp.com/tutorial/category/power-bi)[PySpark](https://www.datacamp.com/tutorial/category/pyspark)[Python](https://www.datacamp.com/tutorial/category/python)[R](https://www.datacamp.com/tutorial/category/r-programming)[Scala](https://www.datacamp.com/tutorial/category/scala)[Snowflake](https://www.datacamp.com/tutorial/category/snowflake)[Spreadsheets](https://www.datacamp.com/tutorial/category/spreadsheets)[SQL](https://www.datacamp.com/tutorial/category/sql)[SQLite](https://www.datacamp.com/tutorial/category/sqlite)[Tableau](https://www.datacamp.com/tutorial/category/tableau)
Category
Topics
Discover content by data science topics
[AI for Business](https://www.datacamp.com/tutorial/category/ai-for-business)[Big Data](https://www.datacamp.com/tutorial/category/big-data)[Career Services](https://www.datacamp.com/tutorial/category/career-services)[Cloud](https://www.datacamp.com/tutorial/category/cloud)[Data Analysis](https://www.datacamp.com/tutorial/category/data-analysis)[Data Engineering](https://www.datacamp.com/tutorial/category/data-engineering)[Data Literacy](https://www.datacamp.com/tutorial/category/data-literacy)[Data Science](https://www.datacamp.com/tutorial/category/data-science)[Data Visualization](https://www.datacamp.com/tutorial/category/data-visualization)[DataLab](https://www.datacamp.com/tutorial/category/datalab)[Deep Learning](https://www.datacamp.com/tutorial/category/deep-learning)[Machine Learning](https://www.datacamp.com/tutorial/category/machine-learning)[MLOps](https://www.datacamp.com/tutorial/category/mlops)[Natural Language Processing](https://www.datacamp.com/tutorial/category/natural-language-processing)[Vector Databases](https://www.datacamp.com/tutorial/category/vector-databases)
[Browse Courses](https://www.datacamp.com/courses-all)
category
1. [Home](https://www.datacamp.com/)
2. [Tutorials](https://www.datacamp.com/tutorial)
3. [Git](https://www.datacamp.com/tutorial/category/git)
# Git Reset and Revert Tutorial for Beginners
Discover how to use Git reset and revert to manage your project history. Practice with detailed examples using soft, mixed, and hard resets. Learn the difference between Git reset and revert.
Contents
Updated Oct 1, 2024 · 10 min read
Contents
- [What is Git?](https://www.datacamp.com/tutorial/git-reset-revert-tutorial#what-is-git?-gitis)
- [Overview of Git Revert and Reset](https://www.datacamp.com/tutorial/git-reset-revert-tutorial#overview-of-git-revert-and-reset%C2%A0-gitre)
- [Git Reset](https://www.datacamp.com/tutorial/git-reset-revert-tutorial#git-reset%C2%A0-<imgs)
- [git reset --soft](https://www.datacamp.com/tutorial/git-reset-revert-tutorial#git-reset---soft-the<c)
- [git reset --mixed](https://www.datacamp.com/tutorial/git-reset-revert-tutorial#git-reset---mixed-thisi)
- [git reset --hard](https://www.datacamp.com/tutorial/git-reset-revert-tutorial#git-reset---hard-thiso)
- [Git Revert](https://www.datacamp.com/tutorial/git-reset-revert-tutorial#git-revert-<imgl)
- [git revert --no-edit \<commit ID\>](https://www.datacamp.com/tutorial/git-reset-revert-tutorial#git-revert---no-edit-<commit-id>-the<c)
- [git revert \<commit hash\>](https://www.datacamp.com/tutorial/git-reset-revert-tutorial#git-revert-<commit-hash>-thisw)
- [Git Revert vs. Reset](https://www.datacamp.com/tutorial/git-reset-revert-tutorial#git-revert-vs.-reset-gitre)
- [Git Revert](https://www.datacamp.com/tutorial/git-reset-revert-tutorial#git-revert%C2%A0-%0A<lia)
- [Git Reset](https://www.datacamp.com/tutorial/git-reset-revert-tutorial#git-reset-%0A<lia)
- [Conclusion](https://www.datacamp.com/tutorial/git-reset-revert-tutorial#conclusion-inthi)
- [Git Reset and Revert FAQs](https://www.datacamp.com/tutorial/git-reset-revert-tutorial#faq)
## Training more people?
Get your team access to the full DataCamp for business platform.
[For Business](https://www.datacamp.com/business)For a bespoke solution [book a demo](https://www.datacamp.com/business/demo-2).
Git was considered in the past to be a tailored tool for software developers because of its ability to allow version control of source codes. This reality is changing because most data scientists, [data engineers](https://www.datacamp.com/certification/data-engineer), and machine learning engineers are also adopting these best practices.
In this tutorial, we will provide you with a clear understanding of Git. Then, we will walk through a practical tutorial of Git revert versus reset, from initializing a project to monitoring the changes.
## Become a Data Engineer
Build Python skills to become a professional data engineer.
[Get Started for Free](https://www.datacamp.com/tracks/professional-data-engineer)
## What is Git?
Git is an open-source version control tool created in 2005 by Linus Torvalds. It is mainly used to efficiently track changes in project files so that project members can have a record of all the changes and merge the changes without losing valuable information. Our [introductory course to Git](https://datacamp.com/courses/introduction-to-git) will give you more insights into how and why to use Git while covering the most common steps in Git workflow.
In addition to that, the [Github Concepts](https://www.datacamp.com/courses/github-concepts) course will provide you with the keys to using Github’s various features, navigating the interface, and successfully performing everyday collaborative tasks.
## Overview of Git Revert and Reset
Git revert and Git reset are two commands used in Git to undo changes to a project code and history, but in different ways. In this section, we will provide an overview of how to use both revert and reset. Before diving into the process, let’s create a Git project.
#### Create a Git project
Below are the instructions to create a git project and initialize it with some files:
```
Powered ByWas this AI assistant helpful? Yes No
```
- `mkdir git_revert_reset` creates a folder called `git_revert_reset`.
- `cd git_revert_reset` moves to the folder.
- `git init .` initializes the folder as a git repository.
Considering this as a data science project, we can create the following files for data acquisition, data preprocessing, and model training using this `touch` command:
```
touch data_acquisition.py data_preprocessing.py model_training.pyPowered ByWas this AI assistant helpful? Yes No
```
This is the final content of the project folder.
```
Powered ByWas this AI assistant helpful? Yes No
```
## Git Reset

Git Reset diagram ([Source](https://www.scmgalaxy.com/tutorials/git-commands-tutorials-and-example-git-reset-git-revert/))
The `git reset` command is used to undo the changes in your working directory and get back to a specific commit while discarding all the commits made after that one.
For instance, imagine you made ten commits. Using `git reset` on the first commit will remove all nine commits, taking you back to the first commit stage.
Before using `git reset`, it is important to consider the type of changes you plan to make; otherwise, you will create more chaos than good.
You can use multiple options along with `git reset`, but these are the main ones. Each one is used depending on a specific situation: `git reset --soft`, `git reset --mixed`, and `git reset --hard`
### git reset --soft
The `--soft` aims to change the `HEAD` (where the last commit is in your local machine) reference to a specific commit. For instance, if we realize that we forgot to add a file to the commit, we can move back using the `--soft` with respect to the following format:
- `git reset --soft HEAD~n` to move back to the commit with a specific reference (n). `git reset --soft HEAD~1` gets back to the last commit.
- `git reset --soft <commit ID>` moves back to the head with the `<commit ID>`.
Let’s look at some examples.
```
Powered ByWas this AI assistant helpful? Yes No
```
This is the result of the previous command.
```
Powered ByWas this AI assistant helpful? Yes No
```
We can check the status of the commit as follows:
```
Powered ByWas this AI assistant helpful? Yes No
```
But, we forgot to add the `model_training.py` script to the commit.
Since the last commit is located at the `HEAD`, we can fix this issue by running the following three statements.
- Get back to the pre-commit phase using `git reset --soft HEAD` allowing git reset file.
- Add the forgotten file with `git add`.
- Make the changes with a final commit `git commit`.
```
Powered ByWas this AI assistant helpful? Yes No
```
The following result made `git reset` to master after all the previous steps:
```
Powered ByWas this AI assistant helpful? Yes No
```
### git reset --mixed
This is the default argument for `git reset`. Running this command has two impacts: (1) uncommit all the changes and (2) unstage them. Imagine that we accidentally added the `model_training.py` file, and we want to remove it because the model training is not finished yet. Here is how to proceed:
- Unstage the files that were in the commit with git reset HEAD
- Only add the files we need for the commit
```
Powered ByWas this AI assistant helpful? Yes No
```
Now we can add only the last two files to be committed and make the commit.
```
Powered ByWas this AI assistant helpful? Yes No
```
### git reset --hard
This option has the potential of being dangerous. So, be cautious when using it\!
Basically, when using the hard reset on a specific commit, it forces the HEAD to get back to that commit and deletes everything else after that.
Before running the hard reset, let's have a look at the content of the folder.
```
Powered ByWas this AI assistant helpful? Yes No
```
Now let's check the content of the folder:
```
Powered ByWas this AI assistant helpful? Yes No
```
We notice that the untracked file *model\_training.py* is deleted. Again, make sure to understand what you are doing with the reset statement because the consequence can be dramatic\!
## Git Revert

Before and After Revert ([Source](https://www.atlassian.com/git/tutorials/resetting-checking-out-and-reverting))
`Git revert` is similar to `git reset`, but the approach is slightly different. Instead of removing all the commits in its way, the revert ONLY undoes a single commit by taking you back to the staged files before the commit.
So, instead of removing a commit, `git revert` inverts the changes introduced by the original commit by creating a new commit with the underlying inverse content. This is a safe way to revoke a commit because it prevents you from losing your history.
It is important to know that the revert will have no effect unless the commit hash or reference is specified.
Let’s explore a few common git revert options.
### git revert --no-edit \<commit ID\>
The `--no-edit` option allows the user to not change the message used for the commit you attend to revert, and this will make git revert file to master branch.
To illustrate this scenario, let’s create two new files to be added.
```
touch model_monitoring.py data_monitoring.pyPowered ByWas this AI assistant helpful? Yes No
```
After creating those files, we need to add them to the modified version staging area.
```
git add model_monitoring.pyPowered ByWas this AI assistant helpful? Yes No
```
Before moving on, let’s have a look at the status by running the following command.
```
Powered ByWas this AI assistant helpful? Yes No
```
We can observe that the two files have been added, now, we can commit the changes.
```
Powered ByWas this AI assistant helpful? Yes No
```
Then, the commit hash can be generated through the following command. This is beneficial when trying to perform an action to this specific commit.
```
Powered ByWas this AI assistant helpful? Yes No
```
Now, let's imagine that the previous commit was not intentional, and we want to get rid of it.
```
Powered ByWas this AI assistant helpful? Yes No
```
Let’s have a look at the log of the commits:
```
Powered ByWas this AI assistant helpful? Yes No
```
From the log, we can notice that a new commit has been created with the ID `c61ef6b4e86f41f47c8c77de3b5fca3945a7c075` to reverse the original commit with the ID `eae84e7669af733ee6c1b854f2fcd9acfea9d4a3`.
In this example, since we have a single commit, we could simply use `git revert HEAD,` which would have the same effect by reverting the last commit.
### git revert \<commit hash\>
This will only remove the changes associated with this commit hash and does not impact any other commit.
*Which git revert do you think will have an effect, considering the previously generated ID?*
If your answer was `c61ef6b4e86f41f47c8c77de3b5fca3945a7c075` then you are right. Considering `eae84e7669af733ee6c1b854f2fcd9acfea9d4a3` will have no effect, since it is replaced by a new one.
Let’s see what happens when trying to perform the revert.
```
Powered ByWas this AI assistant helpful? Yes No
```
As you can see, the two files initially created and committed have been brought back\!
#### git revert --no-commit \<commit ID\>
The purpose of using the `--no-commit` or `-n` option is to prevent git from automatically committing the revert. This can be beneficial if you want to review the changes made by the revert statement before committing them.
Let’s consider the following example:
```
git revert --no-commit c61ef6b4e86f41f47c8c77de3b5fca3945a7c075Powered ByWas this AI assistant helpful? Yes No
```
This will revert the changes made by the commit with the ID `c61ef6b4e86f41f47c8c77de3b5fca3945a7c075` but will not automatically create a new commit to record the revert. Instead, it will leave the changes unstaged so you can review them and decide whether to commit the revert manually.
## Git Revert vs. Reset
### Git Revert
- Using `git revert`, you have the ability to go back to previous states while creating a new commit.
- Go for this option if you want to undo changes on a public branch for safety.
### Git Reset
- With `git reset`, you can go back to the previous commits but you can’t create a new commit.
- This option is better when undoing changes on a private branch.
## Conclusion
In this tutorial, we have seen how to perform `git reset` and `git revert` changes in your git repository. Remember that a hard reset can be very dangerous because it will cause you to lose your untracked files in the project.
So, what is next? DataCano has multiple tutorials that could be a good next step to improve your Git skills, such as:
- [How to Resolve Merge Conflicts in Git Tutorial](https://www.datacamp.com/tutorial/how-to-resolve-merge-conflicts-in-git-tutorial)
- [GitHub, and Git Tutorial for Beginners](https://www.datacamp.com/tutorial/github-and-git-tutorial-for-beginners)
- [Git Push and Pull Tutorial](https://www.datacamp.com/tutorial/git-push-pull)
## Get certified in your dream Data Engineer role
Our certification programs help you stand out and prove your skills are job-ready to potential employers.
[Get your Certification](https://www.datacamp.com/certification/data-engineer)

## Git Reset and Revert FAQs
### What is the difference between git reset and git revert?
`git reset` is used to undo commits in the local repository by moving the branch pointer to a previous commit, effectively discarding any commits that were made after that point. `git revert`, on the other hand, is used to undo changes that have been committed to the repository by creating a new commit that undoes the changes made by the specific commit.
### Should you use git reset hard?
Be cautious when using git reset hard because it can completely destroy any changes and remove them from the local directory.Before using it, be 100% sure about what you are doing.
### When should I use git reset?
git reset is useful when you want to undo commits that have not been pushed to a remote repository. It can be used to remove commits that contain mistakes or that are no longer necessary.
It is important to note that git reset is a local operation and does not affect the remote repository. If you have already pushed the commits that you want to undo, you should use git revert instead.
### When should I use git revert?
git revert is useful when you want to undo commits that have already been pushed to a remote repository. It is a safe option because it does not destroy any commits and leaves a clear record of the changes made in the repository.
It is important to note that git revert can only undo commits that have not been merged into other branches. If you want to undo commits that are part of a merged branch, you should use git reset or another method to remove the commits.
### What is git reset?
`git reset` is used to undo changes in your commit history or working directory. It allows you to reset your HEAD to a specific commit, and optionally modify the index (staging area) and working directory.
### What are the differences between --soft, --mixed, and --hard in git reset?
`--soft`: Moves `HEAD` to the specified commit but keeps changes staged for commit. `--mixed` (default): Moves `HEAD` to the specified commit and unstages the changes (keeps the working directory intact). `--hard`: Moves `HEAD` to the specified commit and discards all changes in the index and working directory.
Topics
[Git](https://www.datacamp.com/tutorial/category/git)
***
[Zoumana Keita](https://www.datacamp.com/portfolio/keitazoumana) A data scientist who likes to write and share knowledge with the data and IA community
***
Topics
[Git](https://www.datacamp.com/tutorial/category/git)
[Git Revert Merge Commit: A Guide With Examples](https://www.datacamp.com/tutorial/git-revert-merge)
[Git Switch Branch: A Guide With Practical Examples](https://www.datacamp.com/tutorial/git-switch-branch)
[Git Squash Commits: A Guide With Examples](https://www.datacamp.com/tutorial/git-squash-commits)
[GitHub and Git Tutorial for Beginners](https://www.datacamp.com/tutorial/github-and-git-tutorial-for-beginners)

[How to Use Git Rebase: A Tutorial for Beginners](https://www.datacamp.com/tutorial/git-rebase-tutorial-for-beginners)
[Git Setup: The Definitive Guide](https://www.datacamp.com/tutorial/git-setup)
Related
[TutorialGit Revert Merge Commit: A Guide With Examples](https://www.datacamp.com/tutorial/git-revert-merge)
Learn how to safely undo a Git merge using \`git revert\`, preserving commit history and resolving potential conflicts.
François Aubry
[TutorialGit Switch Branch: A Guide With Practical Examples](https://www.datacamp.com/tutorial/git-switch-branch)
Learn how to switch a branch in Git using git switch and understand the differences between git switch and git checkout.
François Aubry
[TutorialGit Squash Commits: A Guide With Examples](https://www.datacamp.com/tutorial/git-squash-commits)
Learn how to squash commits on a branch using interactive rebase, which helps maintain a clean and organized commit history.
François Aubry
[TutorialGitHub and Git Tutorial for Beginners](https://www.datacamp.com/tutorial/github-and-git-tutorial-for-beginners)
A beginner's tutorial demonstrating how Git version control works and why it is crucial for data science projects.
[](https://www.datacamp.com/portfolio/kingabzpro)
Abid Ali Awan

[TutorialHow to Use Git Rebase: A Tutorial for Beginners](https://www.datacamp.com/tutorial/git-rebase-tutorial-for-beginners)
Discover what Git Rebase is and how to use it in your data science workflows.
Javier Canales Luna
[TutorialGit Setup: The Definitive Guide](https://www.datacamp.com/tutorial/git-setup)
In this tutorial, you'll learn how to set up Git on your computer in different operating systems.
Olivia Smith
[See More](https://www.datacamp.com/tutorial/category/git)
[See More](https://www.datacamp.com/tutorial/category/git)
## Grow your data skills with DataCamp for Mobile
Make progress on the go with our mobile courses and daily 5-minute coding challenges.
[Download on the App Store](https://datacamp.onelink.me/xztQ/45dozwue?deep_link_sub1=%7B%22src_url%22%3A%22https%3A%2F%2Fwww.datacamp.com%2Ftutorial%2Fgit-reset-revert-tutorial%22%7D)[Get it on Google Play](https://datacamp.onelink.me/xztQ/go2f19ij?deep_link_sub1=%7B%22src_url%22%3A%22https%3A%2F%2Fwww.datacamp.com%2Ftutorial%2Fgit-reset-revert-tutorial%22%7D)
**Learn**
[Learn Python](https://www.datacamp.com/blog/how-to-learn-python-expert-guide)[Learn AI](https://www.datacamp.com/blog/how-to-learn-ai)[Learn Power BI](https://www.datacamp.com/learn/power-bi)[Learn Data Engineering](https://www.datacamp.com/category/data-engineering)[Assessments](https://www.datacamp.com/signal)[Career Tracks](https://www.datacamp.com/tracks/career)[Skill Tracks](https://www.datacamp.com/tracks/skill)[Courses](https://www.datacamp.com/courses-all)[Data Science Roadmap](https://www.datacamp.com/blog/data-science-roadmap)
**Data Courses**
[Python Courses](https://www.datacamp.com/category/python)[R Courses](https://www.datacamp.com/category/r)[SQL Courses](https://www.datacamp.com/category/sql)[Power BI Courses](https://www.datacamp.com/category/power-bi)[Tableau Courses](https://www.datacamp.com/category/tableau)[Alteryx Courses](https://www.datacamp.com/category/alteryx)[Azure Courses](https://www.datacamp.com/category/azure)[AWS Courses](https://www.datacamp.com/category/aws)[Google Cloud Courses](https://www.datacamp.com/category/google-cloud)[Google Sheets Courses](https://www.datacamp.com/category/google-sheets)[Excel Courses](https://www.datacamp.com/category/excel)[AI Courses](https://www.datacamp.com/category/artificial-intelligence)[Data Analysis Courses](https://www.datacamp.com/category/data-analysis)[Data Visualization Courses](https://www.datacamp.com/category/data-visualization)[Machine Learning Courses](https://www.datacamp.com/category/machine-learning)[Data Engineering Courses](https://www.datacamp.com/category/data-engineering)[Probability & Statistics Courses](https://www.datacamp.com/category/probability-and-statistics)
**DataLab**
[Get Started](https://www.datacamp.com/datalab)[Pricing](https://www.datacamp.com/datalab/pricing)[Security](https://www.datacamp.com/datalab/security)[Documentation](https://datalab-docs.datacamp.com/)
**Certification**
[Certifications](https://www.datacamp.com/certification)[Data Scientist](https://www.datacamp.com/certification/data-scientist)[Data Analyst](https://www.datacamp.com/certification/data-analyst)[Data Engineer](https://www.datacamp.com/certification/data-engineer)[SQL Associate](https://www.datacamp.com/certification/sql-associate)[Power BI Data Analyst](https://www.datacamp.com/certification/data-analyst-in-power-bi)[Tableau Certified Data Analyst](https://www.datacamp.com/certification/data-analyst-in-tableau)[Azure Fundamentals](https://www.datacamp.com/certification/azure-fundamentals)[AI Fundamentals](https://www.datacamp.com/certification/ai-fundamentals)
**Resources**
[Resource Center](https://www.datacamp.com/resources)[Upcoming Events](https://www.datacamp.com/webinars)[Blog](https://www.datacamp.com/blog)[Code-Alongs](https://www.datacamp.com/code-along)[Tutorials](https://www.datacamp.com/tutorial)[Docs](https://www.datacamp.com/doc)[Open Source](https://www.datacamp.com/open-source)[RDocumentation](https://www.rdocumentation.org/)[Book a Demo with DataCamp for Business](https://www.datacamp.com/business/demo)[Data Portfolio](https://www.datacamp.com/data-portfolio)
**Plans**
[Pricing](https://www.datacamp.com/pricing)[For Students](https://www.datacamp.com/pricing/student)[For Business](https://www.datacamp.com/business)[For Universities](https://www.datacamp.com/universities)[Discounts, Promos & Sales](https://www.datacamp.com/promo)[Expense DataCamp](https://www.datacamp.com/expense)[DataCamp Donates](https://www.datacamp.com/donates)
**For Business**
[Business Pricing](https://www.datacamp.com/business/compare-plans)[Teams Plan](https://www.datacamp.com/business/learn-teams)[Data & AI Unlimited Plan](https://www.datacamp.com/business/data-unlimited)[Customer Stories](https://www.datacamp.com/business/customer-stories)[Partner Program](https://www.datacamp.com/business/partner-program)
**About**
[About Us](https://www.datacamp.com/about)[Learner Stories](https://www.datacamp.com/stories)[Careers](https://www.datacamp.com/careers)[Become an Instructor](https://www.datacamp.com/learn/create)[Press](https://www.datacamp.com/press)[Leadership](https://www.datacamp.com/about/leadership)[Contact Us](https://support.datacamp.com/hc/en-us/articles/360021185634)[DataCamp Español](https://www.datacamp.com/es)[DataCamp Português](https://www.datacamp.com/pt)[DataCamp Deutsch](https://www.datacamp.com/de)[DataCamp Français](https://www.datacamp.com/fr)
**Support**
[Help Center](https://support.datacamp.com/hc/en-us)[Become an Affiliate](https://www.datacamp.com/affiliates)
[Facebook](https://www.facebook.com/datacampinc/)
[Twitter](https://twitter.com/datacamp)
[LinkedIn](https://www.linkedin.com/school/datacampinc/)
[YouTube](https://www.youtube.com/channel/UC79Gv3mYp6zKiSwYemEik9A)
[Instagram](https://www.instagram.com/datacamp/)
[Privacy Policy](https://www.datacamp.com/privacy-policy)[Cookie Notice](https://www.datacamp.com/cookie-notice)[Do Not Sell My Personal Information](https://www.datacamp.com/do-not-sell-my-personal-information)[Accessibility](https://www.datacamp.com/accessibility)[Security](https://www.datacamp.com/security)[Terms of Use](https://www.datacamp.com/terms-of-use)
© 2026 DataCamp, Inc. All Rights Reserved. |
| Readable Markdown | What is Git? Git is an open-source version control tool created in 2005 by Linus Torvalds. It is mainly used to efficiently track changes in project files so that project members can have a record of all the changes and merge the changes without losing valuable information. Our [introductory course to Git](https://datacamp.com/courses/introduction-to-git) will give you more insights into how and why to use Git while covering the most common steps in Git workflow. In addition to that, the [Github Concepts](https://www.datacamp.com/courses/github-concepts) course will provide you with the keys to using Github’s various features, navigating the interface, and successfully performing everyday collaborative tasks. Overview of Git Revert and Reset Git revert and Git reset are two commands used in Git to undo changes to a project code and history, but in different ways. In this section, we will provide an overview of how to use both revert and reset. Before diving into the process, let’s create a Git project. Create a Git project Below are the instructions to create a git project and initialize it with some files: `mkdir git_revert_reset` creates a folder called `git_revert_reset`. `cd git_revert_reset` moves to the folder. `git init .` initializes the folder as a git repository. Considering this as a data science project, we can create the following files for data acquisition, data preprocessing, and model training using this `touch` command: This is the final content of the project folder. Git Reset  Git Reset diagram ([Source](https://www.scmgalaxy.com/tutorials/git-commands-tutorials-and-example-git-reset-git-revert/)) The `git reset` command is used to undo the changes in your working directory and get back to a specific commit while discarding all the commits made after that one. For instance, imagine you made ten commits. Using `git reset` on the first commit will remove all nine commits, taking you back to the first commit stage. Before using `git reset`, it is important to consider the type of changes you plan to make; otherwise, you will create more chaos than good. You can use multiple options along with `git reset`, but these are the main ones. Each one is used depending on a specific situation: `git reset --soft`, `git reset --mixed`, and `git reset --hard` git reset --soft The `--soft` aims to change the `HEAD` (where the last commit is in your local machine) reference to a specific commit. For instance, if we realize that we forgot to add a file to the commit, we can move back using the `--soft` with respect to the following format: `git reset --soft HEAD~n` to move back to the commit with a specific reference (n). `git reset --soft HEAD~1` gets back to the last commit. `git reset --soft <commit ID>` moves back to the head with the `<commit ID>`. Let’s look at some examples. This is the result of the previous command. We can check the status of the commit as follows: But, we forgot to add the `model_training.py` script to the commit. Since the last commit is located at the `HEAD`, we can fix this issue by running the following three statements. Get back to the pre-commit phase using `git reset --soft HEAD` allowing git reset file. Add the forgotten file with `git add`. Make the changes with a final commit `git commit`. The following result made `git reset` to master after all the previous steps: git reset --mixed This is the default argument for `git reset`. Running this command has two impacts: (1) uncommit all the changes and (2) unstage them. Imagine that we accidentally added the `model_training.py` file, and we want to remove it because the model training is not finished yet. Here is how to proceed: Unstage the files that were in the commit with git reset HEAD Only add the files we need for the commit Now we can add only the last two files to be committed and make the commit. git reset --hard This option has the potential of being dangerous. So, be cautious when using it\! Basically, when using the hard reset on a specific commit, it forces the HEAD to get back to that commit and deletes everything else after that. Before running the hard reset, let's have a look at the content of the folder. Now let's check the content of the folder: We notice that the untracked file *model\_training.py* is deleted. Again, make sure to understand what you are doing with the reset statement because the consequence can be dramatic\! Git Revert  Before and After Revert ([Source](https://www.atlassian.com/git/tutorials/resetting-checking-out-and-reverting)) `Git revert` is similar to `git reset`, but the approach is slightly different. Instead of removing all the commits in its way, the revert ONLY undoes a single commit by taking you back to the staged files before the commit. So, instead of removing a commit, `git revert` inverts the changes introduced by the original commit by creating a new commit with the underlying inverse content. This is a safe way to revoke a commit because it prevents you from losing your history. It is important to know that the revert will have no effect unless the commit hash or reference is specified. Let’s explore a few common git revert options. git revert --no-edit \<commit ID\> The `--no-edit` option allows the user to not change the message used for the commit you attend to revert, and this will make git revert file to master branch. To illustrate this scenario, let’s create two new files to be added. After creating those files, we need to add them to the modified version staging area. Before moving on, let’s have a look at the status by running the following command. We can observe that the two files have been added, now, we can commit the changes. Then, the commit hash can be generated through the following command. This is beneficial when trying to perform an action to this specific commit. Now, let's imagine that the previous commit was not intentional, and we want to get rid of it. Let’s have a look at the log of the commits: From the log, we can notice that a new commit has been created with the ID `c61ef6b4e86f41f47c8c77de3b5fca3945a7c075` to reverse the original commit with the ID `eae84e7669af733ee6c1b854f2fcd9acfea9d4a3`. In this example, since we have a single commit, we could simply use `git revert HEAD,` which would have the same effect by reverting the last commit. git revert \<commit hash\> This will only remove the changes associated with this commit hash and does not impact any other commit. *Which git revert do you think will have an effect, considering the previously generated ID?* If your answer was `c61ef6b4e86f41f47c8c77de3b5fca3945a7c075` then you are right. Considering `eae84e7669af733ee6c1b854f2fcd9acfea9d4a3` will have no effect, since it is replaced by a new one. Let’s see what happens when trying to perform the revert. As you can see, the two files initially created and committed have been brought back\! git revert --no-commit \<commit ID\> The purpose of using the `--no-commit` or `-n` option is to prevent git from automatically committing the revert. This can be beneficial if you want to review the changes made by the revert statement before committing them. Let’s consider the following example: This will revert the changes made by the commit with the ID `c61ef6b4e86f41f47c8c77de3b5fca3945a7c075` but will not automatically create a new commit to record the revert. Instead, it will leave the changes unstaged so you can review them and decide whether to commit the revert manually. Git Revert vs. Reset Git Revert Using `git revert`, you have the ability to go back to previous states while creating a new commit. Go for this option if you want to undo changes on a public branch for safety. Git Reset With `git reset`, you can go back to the previous commits but you can’t create a new commit. This option is better when undoing changes on a private branch. Conclusion In this tutorial, we have seen how to perform `git reset` and `git revert` changes in your git repository. Remember that a hard reset can be very dangerous because it will cause you to lose your untracked files in the project. So, what is next? DataCano has multiple tutorials that could be a good next step to improve your Git skills, such as: [How to Resolve Merge Conflicts in Git Tutorial](https://www.datacamp.com/tutorial/how-to-resolve-merge-conflicts-in-git-tutorial) [GitHub, and Git Tutorial for Beginners](https://www.datacamp.com/tutorial/github-and-git-tutorial-for-beginners) [Git Push and Pull Tutorial](https://www.datacamp.com/tutorial/git-push-pull) |
| Shard | 136 (laksa) |
| Root Hash | 7979813049800185936 |
| Unparsed URL | com,datacamp!www,/tutorial/git-reset-revert-tutorial s443 |