Search notes:
git checkout
The
git checkout
command tells git which revision of the
repository to work with.
After executing the command,
Git will put this revision's files into the working copy tree.
Usually, the command is given a branch name:
git checkout fixFooBarBranch
git checkout master
git checkout 325d4d2
Specifying such a commit id instead of a branch name ends up in a
detached HEAD
git checkout confusion
There are two main functions of
git checkout
:
switching branches
restoring files
The latter works on paths , the former doesn't.
When working on paths, the behavior of git checkout
depends on whether the path is tree-ish or not.
Thus, there are really three «modes of operation» with
git checkout
:
Switching branches
Copying files from the stage to the from the stage to the working tree
Copying files from a tree-ish to the working tree
In order to be able to distinguish between these operation modes, some of these oparations require a «double dash» (--
).
git checkout Rel-2
Start to work on branch named Rel-2
.
git checkout -- file.txt
Copy file.txt
from the stage to working tree .
git checkout Rel-2 -- file.txt
Copy file.txt
from branch named Rel-2
to working tree.
get checkout -- file.txt foo.c
Copy file.txt
and foo.c
from the stage to the working tree
get checkout HEAD file.txt
Discart modifications of file.txt
in both, the stage and the working tree
get checkout HEAD
Two new git commands meant to reduce git checkout confusion
With the intent to reduce the confusion around the
git checkout
command, Git 2.23 introduced two new command:
Undoing an uncommitted modification
Because git checkout
typically expects a branch name, in order to undo uncommitted changes, the path to the file that is to be reverted should be prefixed with two dashes (--
):
git checkout -- path/to/file.c
git checkout -- .
Create a branch and check it out in one go
The -b
command line option names a branch that is first created and then checked out to:
…> git checkout -b fix-v.0.2
Switched to a new branch 'fix-v.0.2'