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
mfaani
  • 33,269
  • 19
  • 164
  • 293

2 Answers2

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

Community
  • 1
  • 1
A.H.
  • 63,967
  • 15
  • 92
  • 126
  • what does "a tracking branch" mean? – mfaani Jun 06 '19 at 21:07
  • @Honey: That's a branch tracking the state of the remote repo. In other words: what `git branch -r` shows you. – A.H. Jun 06 '19 at 21:13
  • 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? – mfaani Jun 06 '19 at 22:45
  • @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. – A.H. Jun 07 '19 at 14:54
  • 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 ` and git would automatically create and checkout the branch for you locally as well. – mfaani Sep 04 '20 at 16:43
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.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • so if there are multiple remote with that name, then it would just fail? What does that look like? – mfaani Jun 05 '19 at 16:30
  • Right. It will fail. The exact text message? Something like the branch does not exist. – eftshift0 Jun 05 '19 at 17:11
  • 2
    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`. – torek Jun 05 '19 at 17:52