Why does git checkout <remote_branchname> not create new tracking branch?
In my case, the problem is that I have a folder with the same name as the branch in the root of my project. Git seems to think the xyz
in git checkout xyz
is the folder, not the branch.
I had to do the following to switch my working tree to branch xyz
for the first time:
git checkout -t origin/xyz
Apparently, this adds a few lines to .git/config
(and maybe other configuration files). After that, I can switch between xyz
and master
just by calling git checkout xyz
/ git checkout master
.
When I'm in the root folder and call git checkout xyz
right after I cloned the repo, git does nothing and doesn't give any response either. When I cd
to a sub-folder so that the folder xyz
is not in scope anymore and then call git checkout xyz
, git complains: error: pathspec 'xyz' did not match any file(s) known to git
.
Clone gives you remotes for all the origin's branches, but it only gives you a local branch for the origin's HEAD
branch (or any other branch on request).
TLDR: If X exists as a remote branch, then git checkout X should create a local branch of it. In my case, for a single branch, it does not.
I'll venture to disagree with that. If you want checkout to create a branch, tell it to create a branch.
git checkout -t origin/release # create branch `release` tracking `origin/release`.git checkout -b foobar # create branch `foobar` based on your HEAD commit
[edit:]
I think I understand the complaint now.
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 because it prioritizes the reset it can choose to do something unsafe when it could have chosen to do something safe.
You are missing the -b
option
Try doing
git checkout -b branch_name
Edit Note: As discussed in comments below, this would be the case only if you were using git version 1.7.9 or lesser, which I had been at the time.