How does git checkout work after git fetch

让人想犯罪 __ 提交于 2019-12-11 14:59:53

问题


I just did

git fetch origin <remoteBranch>

And after that I just did

git checkout <remoteBranch>

That created a local branch with the name of <remoteBranch>.

How does that just work? Normally when I want to create a local branch I have to do

git checkout -b

回答1:


The manual for checkout says:

git checkout <branch>

[...]If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to

  $ git checkout -b <branch> --track <remote>/<branch>

If the branch exists in multiple remotes and one of them is named by the checkout.defaultRemote configuration variable, we’ll use that one for the purposes of disambiguation, even if the <branch> isn’t unique across all remotes. Set it to e.g. checkout.defaultRemote=origin to always checkout remote branches from there if <branch> is ambiguous but exists on the origin remote. See also checkout.defaultRemote in git-config[1].




回答2:


To the best of my knowledge, when you ask to checkout, if the branch does not exist locally, git will try to find one (and only one) remote branch with that name. If it exists and there's a single one (there could be multiple remotes set up on your repo with that same branch name) then git guesses that's the branch you want and so it creates it locally using the remote branch as the upstream branch.



来源:https://stackoverflow.com/questions/56464499/how-does-git-checkout-work-after-git-fetch

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