Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does git checkout <remote_branchname> not create new tracking branch?

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.

The remote repository has a master, release, and some other branches (say refactor-update) I do:

git clone WHATEVER/repo.git
git checkout release

But I do not get the normal branch switch message, nor is a local branch tracking origin/release created (per the man page for git checkout). I am still on master. Note that if I do git checkout refactor-update everything works as expected.

This is reproducible on other computers (though the same platform and likely the same version of git). I have (out of desperation, not because I thought it would work), removed the release branch and replaced it with a backup and master respectively with no change in behaviour (git push origin :release && git push origin master:release)

I am stuck for what might be causing the problem. My suspicion is that git checkout doesn't seem to recognize the word release for some reason, which would make it much like executing just git checkout. To try and follow up on this, in another repo I created a release branch to see if it was a reserved word or something, but it is definitely just in this repository. There doesn't seem to be anything out of the ordinary in .git/config.

Git version: 1.8

like image 258
medwards Avatar asked Sep 16 '13 17:09

medwards


2 Answers

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.

like image 171
jcsahnwaldt Reinstate Monica Avatar answered Sep 30 '22 10:09

jcsahnwaldt Reinstate Monica


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.

like image 39
jthill Avatar answered Sep 30 '22 08:09

jthill