Ga naar hoofdinhoud

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 the two commands.
Bijgewerkt 29 mei 2026  · 8 min lezen

Git started as a tool built for software developers, but it has become standard for anyone working with code. Today, data scientists, data engineers, and machine learning engineers are also adopting these best practices.

In this tutorial, I cover the key differences between git reset and git revert, two Git commands for undoing changes. I walk through practical examples of each mode, from initializing a project to reverting specific commits.

Learn Git Fundamentals Today

For beginners: Master version control using Git.
Start Learning For Free

TL;DR

  • git reset moves the branch pointer backward, removing commits from history. Use it for local cleanup before pushing.

  • git reset --soft keeps changes staged, --mixed (default) unstages them, and --hard discards them entirely.

  • git revert creates a new commit that undoes a previous commit. Use it for changes already pushed to a shared branch.

  • Never use git reset on shared branches. If something is already pushed, use git revert instead.

  • If you accidentally run git reset --hard, check git reflog to recover lost commits.

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

In addition to that, the Introduction to GitHub Concepts course covers GitHub’s key features, navigating the interface, and successfully performing everyday collaborative tasks.

Git revert and Git reset are two commands used in Git to undo changes to a project code and history, but in different ways. 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 .
  • 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

This is the final content of the project folder.

git_revert_reset
      |---------- data_acquisition.py
      |---------- data_preprocessing.py
      |---------- model_training.py

Git Reset 

Diagram of Before and After 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 commit -m "added data acquisition and preprocessing scripts"

This is the result of the previous command. 

[master (root-commit) faf864e] added data acquisition and preprocessing scripts
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 data_acquisition.py
create mode 100644 data_preprocessing.py

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)

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. 

  • Undo the last commit while keeping changes staged using git reset --soft HEAD~1.

  • Add the forgotten file with git add

  • Make the changes with a final commit git commit.

git reset --soft HEAD~1

git add model_training.py

git commit -m "added all the python scripts"

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

For a deeper walkthrough of soft resets, see our Git Reset Soft: Complete Guide. If you only need to fix the last commit message or add forgotten files, git commit --amend is often a quicker alternative to resetting.

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)

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"

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

Now let's check the content of the folder:

ls
data_acquisition.py data_preprocessing.py

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!

If you accidentally run a hard reset, git reflog is your safety net. Git keeps a log of all reference changes for about 30 days, so you can often recover "lost" commits by running git reflog and then git reset --hard HEAD@{n} to jump back to the state before the reset. Note that uncommitted or unstaged changes cannot be recovered this way.

Git Revert

Diagram of Revert - Before and After

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

After creating those files, we need to add them to the modified version staging area.

git add model_monitoring.py

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


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


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

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

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

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 use git revert HEAD, which has the same effect. For more examples, see our guide to 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

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

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 stage the inverse changes so you can review them and decide whether to commit the revert manually.

Git Revert vs. Reset

git reset and git revert both undo changes, but they work differently and suit different situations.

Criteria git reset git revert
How it works Moves the branch pointer backward, discarding commits from history Creates a new commit that inverts a previous commit's changes
Commit history Rewrites history (commits are removed) Preserves full history (adds a new revert commit)
Safe for shared branches? No (collaborators will have conflicts if they pulled the removed commits) Yes (history stays consistent for all collaborators)
Scope Can undo multiple commits at once Undoes one commit at a time (use --no-commit to batch)
Best use case Cleaning up local or private branches before pushing Undoing changes already pushed to a remote
Risk level High with --hard (permanently deletes uncommitted work) Low (no data is ever lost)

As a rule of thumb: use git reset to clean up unpushed local history, and git revert to safely undo anything others may have pulled. For specifics on reverting merge commits, see our Git Revert Merge Commit guide.

Conclusion

The key takeaway: use git reset to clean up local history you haven't pushed yet, and git revert to safely undo changes on shared branches. Be especially careful with git reset --hard, since it permanently removes uncommitted changes from your working directory. 

To continue building your Git skills, check out these related DataCamp tutorials:

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.

Onderwerpen
Gerelateerd

Tutorial

Git Reset Soft: Complete Guide with Examples

Master git reset --soft with practical examples, workflows, and safety protocols. Learn to manage commit history, split commits, and optimize your Git workflow.
Khalid Abdelaty's photo

Khalid Abdelaty

Tutorial

Git Revert Merge Commit: A Guide With Examples

Learn how to safely undo a Git merge using `git revert`, preserving commit history and resolving potential conflicts.
François Aubry's photo

François Aubry

Tutorial

Git Revert Last Commit: How to Safely Undo a Change in Git

Learn how to use git revert to undo your latest Git commit without rewriting history. Understand its effects, compare it with git reset, and handle real-world edge cases.
Oluseye Jeremiah's photo

Oluseye Jeremiah

Tutorial

Git Remote: A Complete Guide with Examples

Learn about Git remotes, their purpose, and how to use them for version control in your project. Includes practical examples.
Mark Pedigo's photo

Mark Pedigo

Tutorial

Git reset HEAD: A Comprehensive Guide

Learn what the Git rest HEAD command is, how it works, and its related safety and recovery considerations.
Austin Chia's photo

Austin Chia

Tutorial

Git Merge Tutorial: A Comprehensive Guide with Examples

Learn how to merge branches efficiently in Git with this step-by-step tutorial. Explore different merge strategies, resolve conflicts, and apply best practices to keep your Git history clean.
Srujana Maddula's photo

Srujana Maddula

Meer zienMeer zien