简体   繁体   中英

How does git checkout work after git fetch

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

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.

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 eg 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].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM