Reset,
Checkout, and Revert
Commit-level Operations
The git reset
, git checkout
, and git revert
command are some of the most useful tools in your Git toolbox. They all let you undo some kind of change in your repository, and the first two commands can be used to manipulate either commits or individual files.
Because they’re so similar, it’s very easy to mix up which command should be used in any given development scenario. In this article, we’ll compare the most common configurations of git reset
, git checkout
, and git revert
. Hopefully, you’ll walk away with the confidence to navigate your repository using any of these commands.
It helps to think about each command in terms of their effect on the three main components of a Git repository: the working directory, the staged snapshot, and the commit history. Keep these components in mind as you read through this article.
Commit-level Operation
The parameters that you pass to git reset
and git checkout
determine their scope. When you don’t include a file path as a parameter, they operate on whole commits. That’s what we’ll be exploring in this section. Note that git revert
has no file-level counterpart.
Reset
On the commit-level, resetting is a way to move the tip of a branch to a different commit. This can be used to remove commits from the current branch. For example, the following command moves the hotfix
branch backwards by two commits.
git checkout hotfix
git reset HEAD~2
The two commits that were on the end of hotfix
are now dangling commits, which means they will be deleted the next time Git performs a garbage collection. In other words, you’re saying that you want to throw away these commits. This can be visualized as the following:
This usage of git reset
is a simple way to undo changes that haven’t been shared with anyone else. It’s your go-to command when you’ve started working on a feature and find yourself thinking, “Oh crap, what am I doing? I should just start over.”
In addition to moving the current branch, you can also get git reset
to alter the staged snapshot and/or the working directory by passing it one of the following flags:
-
--soft
– The staged
snapshot and working directory are not altered in
any way. -
--mixed
– The staged
snapshot is updated to match the specified commit,
but the working directory is not affected. This is
the default option. -
--hard
– The staged
snapshot and the working directory are both updated
to match the specified commit.
It’s easier to think of these modes as defining the scope of a git reset
operation:
These flags are often used with HEAD
as the parameter. For instance, git reset --mixed HEAD
has the affect of unstaging all changes, but leaves them in the working directory. On the other hand, if you want to completely
throw away all your uncommitted changes, you would use git reset --hard HEAD
. These are two of the most common uses of git reset
.
Be careful when passing a commit other than HEAD
to git reset
, since this re-writes the current branch’s history. As discussed in The Golden Rule of Rebasing, this is a big problem when working on a public branch.
Checkout
By now, you should be very familiar with the commit-level version of git checkout
. When passed a branch name, it lets you switch between branches.
git checkout hotfix
Internally, all the above command does is move HEAD
to a different branch and update the working directory to match. Since this has the potential to overwrite local changes, Git forces you to commit or stash any changes in the working directory that will be lost during the checkout operation. Unlike git reset
, git checkout
doesn’t move any branches around.