I have in my git repo, a file named xyz. Coincidently, I also have a branch named xyz. Presently I am on master, but I want to checkout to branch xyz. The command to be used is simple
$ git checkout xyz
But this would checkout the file xyz
to the present HEAD. How would I change my branch to branch xyz
?
As illustrated by commit a047faf (git 1.8.4.3+), you can also try:
git checkout xyz --
(Note: the error message will be clearer with Git 2.21, Q1 2019)
That would make clear that the xyz
part is a branch or commit, while everything after --
must be a path (here no path is provided). See more here on the double-hyphen convention.
If you try without the '--
', that might or might not work, as shown in "Why does git checkout <remote_branchname>
not create new tracking branch?":
git checkout name
does:
- if it's local branch or explicit remote branch, switch to it.
- if it's a tracked path, reset it
- if it's a remote branch, create a tracking branch and switch to it.
And its behavior isn't always the same. Hence the '--
' to provide a clear disambiguation.
While VonC's solution works, I can never remember the syntax, so I typically use a more low-tech solution:
$ (cd somedir && git checkout my-branch)
Or, if you don't have any subdirectories:
$ (cd .git && git -C .. checkout my-branch)
It's easier to remember and it works ;-)
Git 2.21 (Q1 2019, 4+ years later) will clarify the error message and make suggestions
"git checkout frotz
" (without any double-dash that I suggested initially) avoids ambiguity by making sure 'frotz
' cannot be interpreted as a revision and as a path at the same time.
This safety has been updated to check also a unique remote-tracking branch 'frotz
' in a remote, when dwimming to create a local branch 'frotz
' out of a remote-tracking branch 'frotz
' from a remote.
Note: "dwim" (used below) is "do what I mean", when a computer system attempts to anticipate what users intend to do, correcting trivial errors automatically rather than blindly executing users' explicit but potentially incorrect inputs.
See commit be4908f (13 Nov 2018) by Nguyễn Thái Ngọc Duy (pclouds
).
(Merged by Junio C Hamano -- gitster
-- in commit 8d7f9db, 04 Jan 2019)
checkout
: disambiguate dwim tracking branches and local files
When checkout dwim is added in commit 70c9ac2, it is restricted to only dwim when certain conditions are met and fall back to default checkout behavior otherwise.
It turns out falling back could be confusing.
One of the conditions to turn
git checkout frotz
to
git checkout -b frotz origin/frotz
is that frotz
must not exist as a file.
But when the user comes to expect "git checkout frotz
" to create the branch "frotz
" and there happens to be a file named "frotz
", git's silently reverting "frotz
" file content is not helping.
This is reported in Git mailing list and even used as an example of "Git is bad" elsewhere.
We normally try to do the right thing, but when there are multiple "right things" to do, it's best to leave it to the user to decide.
Check this case, ask the user to to disambiguate:
- "
git checkout -- foo
" will check out path "foo"
- "
git checkout foo --
" will dwim and create branch "foo
" 6
For users who do not want dwim, use --no-guess
. It's useless in this
particular case because "git checkout --no-guess foo --
" will just fail.
But it could be used by scripts.
The man page for git checkout
now includes:
--no-guess:
Do not attempt to create a branch if a remote tracking branch of the same name exists.
You're wrong. It will checkout branch xyz.
To checkout a file, you need to use command git checkout -- xyz
.
Git just allow you a shortcut for files if there is no branch with the same name.
See git checkout --help
for details.