How does git checkout work after git fetch
Asked Answered
A

2

1

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
Abecedarian answered 5/6, 2019 at 16:25 Comment(0)
C
3

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

Chancre answered 5/6, 2019 at 18:4 Comment(5)
what does "a tracking branch" mean?Abecedarian
@Honey: That's a branch tracking the state of the remote repo. In other words: what git branch -r shows you.Chancre
I see a bunch of remote branches. The only one different is: origin/HEAD -> origin/develop. Why is this one different? Also Does that mean that I'm tracing all those branches as in when I do git fetch --all then it will fetch/update each of them?Abecedarian
@Honey: origin/HEAD is not a plain ref (i.e. does not point to a commit directly) but a symbolic ref - just as HEAD is in YOUR clone. It is used as a default branch after git clone for checkout. To the last Q: git fetch will manage these branches. git fetch --all is just fetches all remotes, not just one.Chancre
Just learned that it works the same for git pull. Obviously because a git pull also contains a git fetch, then similarly you can do git pull then git checkout <newBranch> and git would automatically create and checkout the branch for you locally as well.Abecedarian
L
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.

Loesch answered 5/6, 2019 at 16:28 Comment(3)
so if there are multiple remote with that name, then it would just fail? What does that look like?Abecedarian
Right. It will fail. The exact text message? Something like the branch does not exist.Loesch
The documentation (or at least the source) calls this "DWIM", for Do What I Mean: git checkout asdf => git checkout -b asdf origin/asdf --track, by default, provided there's just the one matching origin/asdf and no current asdf.Collins

© 2022 - 2024 — McMap. All rights reserved.