1

I know that I can see which upstream branch is being tracked by a local branch by running git branch double verbose:

dino@DINO:$ git branch -vv
  master         b567464 [origin/master] mav cross point example
  p516p          198bf21 [joesmith/master] start adding docs
  p516test       198bf21 start adding docs
* pull_507       4ceafac [soandso/master] restore blah blah blah.
  zorder         13f8d22 [origin/zorder] initial `zorder` tests

The remote tracking branch (if there is one) is shown in square brackets.

My question is: where is this information, this association between the local branch and the remote branch, stored?

I've poked around under .git/refs/ and .git/remotes/, but I am unable to find anything, for example, that lists the association between branch p516p with remote joesmith/master.

(Notice that branch p516test points to the same reference as p516p, but p516test is not tracking any remote. I set it up that way on purpose, in hopes of finding where the tracking remote is stored, by comparing information for p516p with that for p516test; no luck so far).

Daniel Goldfarb
  • 6,937
  • 5
  • 29
  • 61

1 Answers1

4

It's in .git/config. For example:

[branch "main"]
    remote = origin
    merge = refs/heads/main
knittl
  • 246,190
  • 53
  • 318
  • 364
  • Thanks! Interesting ... I would not have thought to look there. Based on the name, I would have [incorrectly] guessed that `./git/config` is modified only by the `git config` command. :-\ – Daniel Goldfarb Apr 29 '22 at 17:37
  • 1
    @DanielGoldfarb git branch/checkout/switch, git remote, and git gui come to mind. – knittl Apr 29 '22 at 17:59
  • There's also a third piece: under `remote "origin"` you'll find one or more `fetch` lines. The default is to have one `remote.origin.fetch` set to `+refs/heads/*:refs/remotes/origin/*`. This provides a mapping scheme: their `main` becomes your `origin/main`, for instance. Then `remote=origin` here, plus `merge=refs/heads/main` here, tells your Git to run the mapping to come up with `refs/remotes/origin/main`. If you change the `fetch=` setting, the mapping changes! It can get very weird. I recommend against changing from the defaults because the results are too surprising. – torek Apr 30 '22 at 01:48
  • 1
    This does, however, point out the difference between a *remote-tracking name* (which is specific to *your* repository) and something we might call a "remote branch name": the *branch name as seen in the remote*. Git's documentation is a bit sloppy in places though, as are human habits, so "remote branch" often means one thing or the other, interchangeably, in people's heads, and you might not even realize you're switching meanings as you switch meanings. – torek Apr 30 '22 at 01:51