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

蓝咒 提交于 2020-01-03 09:03:29

问题


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


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


You're missing the -t option.

git checkout -t origin/release

will create and check out the local branch release that tracks origin/release.

With newer versions of Git, this is enough:

git checkout release


来源:https://stackoverflow.com/questions/18833617/why-does-git-checkout-remote-branchname-not-create-new-tracking-branch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!