2

By a given name of a Git remote-tracking branch, for example, upstream/develop how to find which local branch tracks it if any?

If possible I'm looking for a solution that not relies on shell scripting and also works on Windows.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
bobeff
  • 3,543
  • 3
  • 34
  • 62

3 Answers3

6

An alternative is to use a conditional format with for-each-ref

git for-each-ref --format="%(if:equals=upstream/develop)%(upstream:short)%(then)%(refname:short)%(end)" refs/heads | sort -u

which could be more conveniently put into an alias like

git config --global alias.who-tracks '!f() { git for-each-ref --format="%(if:equals=upstream/$1)%(upstream:short)%(then)%(refname:short)%(end)" refs/heads | sort -u; }; f'

# then when you need it :
git who-tracks develop
git who-tracks another/branch

In this alias, I assumed a unique remote, but of course if you want to be able to use it on different remotes, tweak a little bit to include the remote name in the parameter :

git config --global alias.who-tracks '!f() { git for-each-ref --format="%(if:equals=$1)%(upstream:short)%(then)%(refname:short)%(end)" refs/heads | sort -u; }; f'

# then when you need it :
git who-tracks upstream/develop
git who-tracks origin/another/branch
Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • 1
    I don't think you really need the `sort -u` here, and dropping it would probably help out on Windows (not that I use Windows so I don't really know for sure :-) ). – torek Dec 09 '20 at 20:59
  • 1
    @torek True, but when I tried it without, the blank lines where kinda inesthetic and cumbersome. I guess it's an option ^^ – Romain Valeri Dec 10 '20 at 07:05
5

Another alternative is to filter the very verbose output of the simple git branch with grep

git branch -vv | grep upstream/develop
Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • 1
    My use case was to find the remote and branch that an independent upstream had used for their copy of my branch, based on a partial name for that branch ("po/glos.."). The `git branch -rl | grep po/glos` command was the generic solution to find it, based on your example! – Philip Oakley Oct 20 '22 at 11:34
2

Based on this answer (branch enumeration) and this answer (retrieving upstream branch), you can iterate through local branches and check if any of them has the desired tracking remote branch:

git for-each-ref --shell \
  --format='test %(upstream:short) = "upstream/develop" && echo %(refname:short)' \
  refs/heads/ | sh
iBug
  • 35,554
  • 7
  • 89
  • 134
  • 1
    Why `--shell` and `| sh`? – Romain Valeri Dec 09 '20 at 16:24
  • 2
    @RomainValeri From [the man page}(https://git-scm.com/docs/git-for-each-ref#Documentation/git-for-each-ref.txt---shell), `--shell` only makes substitution (`%(whatever)`) properly shell-quoted for direct evaluation, but does not call a shell by itself. The same holds for `--perl`, `--python` and `--tcl`. – iBug Dec 09 '20 at 16:24
  • 1
    Thanks for the feedback, it's clearer to me now ^^ Never used the option before – Romain Valeri Dec 09 '20 at 16:37