Differences between Git Reset, Revert and Checkout commands
This article discusses the differences between the git reset
, , git revert
and git checkout
commands. These are some of the most useful Git utilities that allow us to undo certain changes in our repository.
It’s easy to get confused with these commands, but by the end of this article, you’ll feel confident using the above commands to use and navigate your repositories.
Differences between Git Reset, Git Revert, and Git Checkout commands
It's easier to understand these commands if we're clear about how each command affects the three main components of a Git repository.
- Working Directory
- Staging snapshots
- We can also refer to the above components as three trees.
Git checkout
What does it mean to check out?
This operation repositions the HEAD pointer to the specified commit.
The figure above shows a series of commits in a Git repository. The HEAD and master branch pointers are currently at commit d.
git checkout
We can move our HEAD ref to any commit
using the command.
For example, to move our HEAD ref to commit b, we would run:
$ git checkout -b
We can use git checkout
the command at both commit and file level. Checking out at file level will update the contents of the file with the contents of the specified commit.
Git revert
When reverting, we take a specific commit and create a new commit to reverse the effect of the specified commit. git revert commit
It only works at the commit level and does not have file-level functionality.
Git reset
When resetting, we make a specific commit, reset the three trees, and update the repository to match the state of the repository at the specified commit. We can reset in three different modes corresponding to the three trees.
We usually use git reset
and git checkout
to undo local or private changes. They both modify the history of the repository and may cause conflicts when pushed to a remote public or shared repository.
Git Reset vs. Git Revert vs. Git Checkout
The following table gives some common use cases for these three commands.
Order | scope | Common use cases |
---|---|---|
git reset | Submission Level | Remove commits in your local branch or discard uncommitted changes. |
git reset | File Level | Unstage a file from the index. |
git checkout | Submission Level | Check out old commits and switch between branches. |
git checkout | File Level | Discard changes in the working directory. |
git revert | Submission Level | Reverse the effects of commits in the public branch. |
git revert | File Level | (not applicable) |
Submit Level Operations
We pass arguments to the git reset
and git checkout
commands to invoke different levels of operations. If we fail to include the file argument, the command will operate on the commit as a whole.
Reset Submissions
Resetting on commit level will move the HEAD ref to the specified commit. We can use this command to remove commits from a branch.
Here is an example.
$ git reset HEAD~3
The command above will move the tip of our branch back three commits. We can call them dangling or orphaned commits.
We have discarded these three commits. It is best to use this command to undo commits that we have not yet published to the remote shared repository.
We can also use the git reset command to change the staging snapshot and the working directory by passing one of the following flags.
- --soft - Do not change staged changes or the working directory.
- --mixed - does not affect the working directory, but changes the staged snapshot to match the specified commit.
- --hard - Change the working directory and staged snapshot to match the specified commit.
Check out old commits
We can git checkout
check the state of the repository at a specific commit using the command. We can also switch between branches by using the branch name.
Here is an example.
$ git checkout feature
The command above will switch to the feature branch. It will not move the branch around.
We can also checkout an arbitrary commit by passing a commit hash or ref instead of a branch name. Here is an example.
$ git checkout HEAD~1
The above command will check the parent of our current commit. Passing HEAD~2 will check the grandparent.
The above command will switch us to detached HEAD mode, since our current HE****AD has no branch references.
All changes and commits done in detached HEAD mode will be inaccessible once you switch to another branch. Always create a new branch when you want to commit changes in detached HEAD state.
Restore a shared submission
When we revert a commit, we create a new commit that reverses the effect of the specified commit. In this way, we avoid rewriting the commit history of the shared repository.
For example, if we wanted to revert the most recent commit, we would run:
$ git revert HEAD
File-level operations
git checkout
The and git reset
commands accept a file path as an optional argument. The effects of these commands are limited to a single file.
Reset Files
We can update the staged snapshot of a file to match the version at a specified commit as follows.
$ git reset HEAD~1 README.md
The command above will get the version of the README.md file in the parent commit of the current commit and add it to the index of the next commit. We can run git reset HEAD README.md
to unstage the README.md file.
Git Checkout Files
Passing a file path to git checkout
the command updates the working directory instead of staging a snapshot. This does not move the HEAD pointer between branches.
Here is an example.
$ git checkout HEAD~1 README.md
This will take the version of the README.md file from the parent commit of our current commit and update only the README.md file in our working directory. It simply means that we have reverted to the version of the file in the old commit.
Unlike the command , which undoes the changes introduced by a specified commit git revert
, this discards all subsequent changes. We can use this to discard unstaged changes to a single file, as shown below.
$ git checkout HEAD README.md
In summary, we can easily distinguish between git reset , git revert , and git checkout commands by understanding the impact of each command on the commit history, staging snapshot, and working directory.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Git revert multiple commits
Publish Date:2025/04/02 Views:108 Category:Git
-
In this article, we will see how to revert multiple commits in Git. In Git, we may have completed many commits for some feature development, or submitted some bug fixes. We might now decide to abandon this work. Therefore, to restore the re
Git resets local branch to branch in remote repository
Publish Date:2025/04/01 Views:151 Category:Git
-
This article will show how to reset a local branch in a local repository to resemble a branch on a remote repository in git. Alternatively, we can discard any untracked changes in the local repository. Often, we have a local branch with som
Undoing a Git reset using the --hard flag
Publish Date:2025/04/01 Views:121 Category:Git
-
Sometimes you may want to reset your changes to a specific commit. Suppose you chose git reset --hard commit id the -p way to reset your changes but forgot --hard the -p flag would discard uncommitted changes on your local system and later
Git revert local commits
Publish Date:2025/03/30 Views:85 Category:Git
-
When a software engineer or a web developer uses Git, it is obvious that he pushes a lot of code and commits to the Git repository every day, and while doing so, the need to undo or revert a particular commit or a set of commits arises from
Difference between Git RM --Cached and Git Reset File
Publish Date:2025/03/28 Views:127 Category:Git
-
This article discusses the difference between the git rm --cached and it resetfile commands. We will discuss the functionality of each command to understand the differences between the two. Difference between git rm --cached and git reset f
Git RM --Cached 和 Git Reset File 的区别
Publish Date:2023/04/05 Views:255 Category:Git
-
本文讨论 git rm --cached 和 it reset
命令之间的区别。 我们将讨论每个命令的功能,以了解两者之间的差异。
Git Reset、Revert 和 Checkout 命令之间的区别
Publish Date:2023/03/31 Views:219 Category:Git
-
本文讨论了 git reset 、git revert 和 git checkout 命令之间的区别。 这些是一些最有用的 Git 实用程序,它们允许我们撤消存储库中的某些更改。
Git 还原多次提交
Publish Date:2023/03/30 Views:158 Category:Git
-
在本文中,我们将了解如何在 Git 中恢复多个提交。在 Git 中,我们可能已经为某些功能开发完成了许多提交,或者提交了一些错误修复。