I've always thought of git reset
and git checkout
as the same, in the sense that both bring the project back to a specific commit. However, I feel they can't be exactly the same, as that would be redundant. What is the actual difference between the two? I'm a bit confused, as the svn only has svn co
to revert the commit.
VonC and Charles explained the differences between git reset
and git checkout
really well. My current understanding is that git reset
reverts all of the changes back to a specific commit, whereas git checkout
more or less prepares for a branch. I found the following two diagrams quite useful in coming to this understanding:
From http://think-like-a-git.net/sections/rebase-from-the-ground-up/using-git-cherry-pick-to-simulate-git-rebase.html, checkout and reset can emulate the rebase.
git checkout bar
git reset --hard newbar
git branch -d newbar
This question is related to
git
git-checkout
git-reset
brief mnemonics:
git reset HEAD : index = HEAD
git checkout : file_tree = index
git reset --hard HEAD : file_tree = index = HEAD
In their simplest form, reset
resets the index without touching the working tree, while checkout
changes the working tree without touching the index.
Resets the index to match HEAD
, working tree left alone:
git reset
Conceptually, this checks out the index into the working tree. To get it to actually do anything you would have to use -f
to force it to overwrite any local changes. This is a safety feature to make sure that the "no argument" form isn't destructive:
git checkout
Once you start adding parameters it is true that there is some overlap.
checkout
is usually used with a branch, tag or commit. In this case it will reset HEAD
and the index to the given commit as well as performing the checkout of the index into the working tree.
Also, if you supply --hard
to reset
you can ask reset
to overwrite the working tree as well as resetting the index.
If you current have a branch checked out out there is a crucial different between reset
and checkout
when you supply an alternative branch or commit. reset
will change the current branch to point at the selected commit whereas checkout
will leave the current branch alone but will checkout the supplied branch or commit instead.
Other forms of reset
and commit
involve supplying paths.
If you supply paths to reset
you cannot supply --hard
and reset
will only change the index version of the supplied paths to the version in the supplied commit (or HEAD
if you don't specify a commit).
If you supply paths to checkout
, like reset
it will update the index version of the supplied paths to match the supplied commit (or HEAD
) but it will always checkout the index version of the supplied paths into the working tree.
One simple use case when reverting change:
1. Use reset if you want to undo staging of a modified file.
2. Use checkout if you want to discard changes to unstaged file/s.
The two commands (reset and checkout) are completely different.
checkout X
IS NOT reset --hard X
If X is a branch name,
checkout X
will change the current branch
while reset --hard X
will not.
The key difference in a nutshell is that reset
moves the current branch reference, while checkout
does not (it moves HEAD).
As the Pro Git book explains under Reset Demystified,
The first thing
reset
will do is move what HEAD points to. This isn’t the same as changing HEAD itself (which is whatcheckout
does);reset
moves the branch that HEAD is pointing to. This means if HEAD is set to themaster
branch (i.e. you’re currently on themaster
branch), runninggit reset 9e5e6a4
will start by makingmaster
point to9e5e6a4
. [emphasis added]
See also VonC's answer for a very helpful text and diagram excerpt from the same article, which I won't duplicate here.
Of course there are a lot more details about what effects checkout
and reset
can have on the index and the working tree, depending on what parameters are used. There can be lots of similarities and differences between the two commands. But as I see it, the most crucial difference is whether they move the tip of the current branch.
Source: Stackoverflow.com