3

How to get the remote name from a local branch?

Note that I do not want the remote tracking branch name but just the remote name.

Consider this contrived example. How do I get prefix/github?

$ git status -b
## dev...prefix/github/rm/dev

$ git remote
github
prefix
prefix/github

So prefix/github is the remote and local branch dev is tracking remote branch rm/dev.

So far I know this way but it is not fullproof.

$ git rev-parse --abbrev-ref @{u} 2>/dev/null | cut -d'/' -f1
prefix

The prefix/github remote name is intentional. I can live with keeping my remote without forward slash (in which case the above would work) but wanted to know if I'm missing something.

fyi: I have a bash script where this eventually would be used.

Update:

See this comment for why this question is not a dupe: Get remote name from local branch

Community
  • 1
  • 1
hIpPy
  • 4,649
  • 6
  • 51
  • 65
  • Are you trying to figure out where the remote branch which tracks a local branch lives? – merlin2011 Apr 15 '17 at 21:09
  • Yes. So in above example I need to get `prefix/github` which is the remote name. – hIpPy Apr 15 '17 at 21:13
  • http://git-wt-commit.rubyforge.org/ directly, or replicate what it's doing. – bishop Apr 15 '17 at 21:19
  • What about `git branch -vv`? – Lasse V. Karlsen Apr 15 '17 at 21:20
  • What if you have multiple remotes? Which name would you like then? – Lasse V. Karlsen Apr 15 '17 at 21:21
  • @lasse-v-karlsen, It's not a dupe. Please read the question again / carefully. `git status -b` gives me the info that I require instead of `git branch -vv` which is superfluous. – hIpPy Apr 15 '17 at 21:38
  • @lasse-v-karlsen, If there are multiple remotes, then `git status -b` shows the last remote info for the branch. – hIpPy Apr 15 '17 at 21:41
  • It seems this works: `git config --get branch.dev.remote`. – hIpPy Apr 15 '17 at 21:43
  • 1
    You said "The `prefix/github` remote name is intentional", but you also have a remote named `prefix`. Don't do this: it is a trap. If you `git fetch` from both remotes `prefix` *and* `prefix/github`, Git's assumption that remote-tracking branch names are unique and determined solely by the remote name, breaks. It will work fine for a while, and then break. (Note that you can keep the name `prefix/github`, as long as you don't *also* use the name `prefix` by itself.) – torek Apr 15 '17 at 22:23
  • Meanwhile, yes, `git config --get branch.dev.remote` is the correct answer ... and is in the answer that this is a not-quite-duplicate of. :-) – torek Apr 15 '17 at 22:25
  • @torek, Yes, you are correct. I just made those up to make a point that simple grep is not sufficient. It will break fetch eventually. Good catch. The example still holds if I have `prefix-test/github` instead of `prefix/github`. – hIpPy Apr 15 '17 at 23:19

2 Answers2

3

This works.

git config --get branch.dev.remote

The local git config file (.git/config) should have this branch section:

[branch "dev"]
    remote = prefix/github
    merge = refs/heads/dev

I'm honestly surprised it is this contrived.

hIpPy
  • 4,649
  • 6
  • 51
  • 65
  • I am not sure what you mean by *contrived*, but it is this way for historical reasons. See http://stackoverflow.com/a/42033047/1256452 for a bit more. (I am sure I have written "more of more" about this elsewhere but could not find it quickly.) – torek Apr 15 '17 at 23:59
  • @torek, Ah I see. I thought it would have been through `git branch` or `git remote` and not `git config` (which is not intuitive). – hIpPy Apr 16 '17 at 00:15
  • The user-oriented porcelain today tries to hide the fact that there are two parts to these things. Whether that's a good design decision is another question entirely. Meanwhile the plumbing code is chock full of historical oddities. :-) – torek Apr 16 '17 at 00:18
  • Ah, I found the longer thing I wrote recently that describes this in more detail: http://stackoverflow.com/a/43318586/1256452 – torek Apr 16 '17 at 00:47
  • Does not work on a freshly created and unpushed Branch. – Akito Oct 04 '21 at 09:58
-1

The information you are looking for is stored in the .git/config file after you setup tracking for a branch. You should be able to extract it from there easily (e.g., grep -B ... | grep ... - please fill in the dots yourself after having a look in that file).

Note that there can be branches without remote tracking information stored explicitly there.

AnoE
  • 8,048
  • 1
  • 21
  • 36