Search code examples
gitgit-checkoutgit-fetch

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

Solution

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