244

I would like to know if there's a way to tell which local branch is tracking which remote branch in Git.

I'm using one remote server, which is named "origin".

1
  • Correct me if I'm wrong however isnt' it a remote branch that is named origin by default rather than the server?? May 16, 2016 at 3:41

10 Answers 10

202

Using the example of my copy of Puppet checked out from the upstream Git repository on Github.com...

$ git remote show origin
* remote origin
  Fetch URL: git://github.com/reductivelabs/puppet.git
  Push  URL: git://github.com/reductivelabs/puppet.git
  HEAD branch: master
  Remote branches:
    0.24.x                 tracked
    0.25.x                 tracked
    2.6.x                  tracked
    master                 tracked
    next                   tracked
    primordial-ooze        tracked
    reins-on-a-horse       tracked
    testing                tracked
    testing-17-march       tracked
    testing-18-march       tracked
    testing-2-april        tracked
    testing-2-april-midday tracked
    testing-20-march       tracked
    testing-21-march       tracked
    testing-24-march       tracked
    testing-26-march       tracked
    testing-29-march       tracked
    testing-31-march       tracked
    testing-5-april        tracked
    testing-9-april        tracked
    testing4268            tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

Then if I were to execute the following:

$ git checkout -b local_2.6 -t origin/2.6.x 
Branch local_2.6 set up to track remote branch 2.6.x from origin.
Switched to a new branch 'local_2.6'

And finally re-run the git remote show origin command again I will then see the following down near the bottom:

  Local branches configured for 'git pull':
    local_2.6 merges with remote 2.6.x
    master    merges with remote master
4
  • 1
    So does this mean you can track all the remote branches in puppet although you have a few local branches. What's the many "tracked" signs mean you see in the result of the command? "tracked" by which local branch?
    – PJ.
    Aug 28, 2010 at 1:27
  • The remote branches are tracked in that if you do a git fetch or git pull updates to the remote branches will be tracked in your cloned repository. The local branches are just that, local branches of the remote branches and thus updates to the remote branches will be tracked and merged in when the appropriate command to do so is given. I explicitly include the '-t' option when making the local branch to ensure it tracks the branch from which it originated from. Remember a local branch can also track another local branch so doesn't have to be a remote branch. Aug 28, 2010 at 4:20
  • 5
    @PJ: The term “track” has two distinct meanings in Git. The “tracked” lines in git remote show remote-name refer to “tracking branches” (snapshots of branches from remote repositories). The “merges with” lines refer to local branches that have an “upstream branch” configuration (made with with the --track/-t option of git branch or git checkout and thus often confused with “tracking branches”). Aug 28, 2010 at 5:40
  • "remote-tracking branch" is the up-to-date glossary entry for "tracking branches" mentioned above. The doc was updated in 8b3f3f84.
    – ento
    Sep 12, 2013 at 4:43
132

For all branches:

git branch -avv

For local branches only:

git branch -lvv

For remote branches only:

git branch -rvv

shows you all branches as well as the name of the upstream branch.

10
  • 2
    This is by far the simplest and fullest answer!
    – user114812
    Mar 21, 2012 at 10:25
  • 6
    On git version 1.7.7.5 this is showing me the local branch and the sha-1 it points to, but it doesn't show the tracked remote branch...
    – mpontillo
    Apr 6, 2012 at 19:44
  • Works for me on git version 1.7.4.1 The second 'v' is required to show the remote tracking branch. Jul 25, 2012 at 11:21
  • 4
    to reduce output git branch -lvv show just local branches with upstream might be useful
    – A B
    Nov 27, 2012 at 23:56
  • 6
    ^git branch -vv works for me...
    – notacouch
    Mar 21, 2013 at 20:48
52

Jeremy Bouse illustrates how git remote show displays tracking information. That should be sufficient if you only want the information for human consumption.

If you plan on using the information in an automated context (e.g. a script) you should use the lower-level (“plumbing”) git for-each-ref instead.

% git remote show origin
* remote origin
⋮
  Local branches configured for 'git pull':
    master merges with remote master
    pu     merges with remote pu
⋮
% git for-each-ref --format='%(refname:short) <- %(upstream:short)' refs/heads
master <- origin/master
pu <- origin/pu

The git for-each-ref learned the %(upstream) token in Git 1.6.3. With earlier versions of Git you will have to extract the tracking information with git config branch.<name>.remote and git config branch.<name>.merge (probably using git for-each-ref to build the commands for each local branch name).

2
  • Your answers output is much more succinct and easier to follow, so you get the up vote :)
    – CubanX
    Jan 12, 2012 at 19:58
  • To make it less succinct, but provide a nice way to visually check that remote names are the same as the local branch names, a common git gotcha: in bash git for-each-ref --format=$'\n'' '' '' '' '' '' ''/%(refname:short);%(upstream:short)' refs/heads | tr ';' $'\n'
    – hobs
    May 15, 2013 at 17:05
20

For a particular branch, you can use git rev-parse with the @{u} or @{upstream} suffix on the branch name, e.g.:

$  git rev-parse --symbolic-full-name master@{u}
refs/remotes/github-mhl/master

... or for the abbreviated form, add --abbrev-ref

$ git rev-parse --symbolic-full-name --abbrev-ref master@{u}
github-mhl/master

You can generally use the branch@{upstream} syntax wherever a commit is expected.

1
  • 1
    +1 Your answer gave me exactly what I needed: git rev-parse --symbolic-full-name HEAD vs. git rev-parse --symbolic-full-name HEAD@{u}, thanks!
    – Tino
    Jul 4, 2012 at 16:55
11

I use the following shell script (named git-tracks) to show the remote branch that is tracked by the current branch:

#!/bin/sh -e
branch=$(git symbolic-ref HEAD)
branch=${branch##refs/heads/}
remote=$(git config "branch.${branch}.remote")
remoteBranch=$(git config "branch.${branch}.merge")
remoteBranch=${remoteBranch##refs/heads/}

echo "${remote:?}/${remoteBranch:?}"

This could also use the mentioned git for-each-ref, but I found the direct access somewhat simpler than filtering the output for the current branch.

3
  • You might consider using "set -e" at the top of the script. That will allow you to remove all of the instances of "|| exit $?" while retaining the same fail-early behavior. Jul 31, 2012 at 21:26
  • @JohnWhitley: Thanks, I've edited my answer. I know about set -e, but usually stick to the explicit checking. But in this case, it's really better. Aug 1, 2012 at 6:39
  • Not working for me on git version 1.9.4. Echos nothing :(
    – Ain
    Sep 3, 2014 at 15:57
10

.git/config file also will provide the tracking branch info as

[remote "Hub"]
    url = ssh://xxxx/tmp/Hub
    fetch = +refs/heads/*:refs/remotes/Hub/*
[branch "develop"]
    remote = Hub
    merge = refs/heads/develop
[branch "Dev1"]
    remote = Test
    merge = refs/heads/Dev1
[remote "Test"]
    url = ssh://xxxx/tmp/gittesting/Dev1GIT
    fetch = +refs/heads/*:refs/remotes/Test/*
1
  • This (I think) answers the question: "What URL is being used for the tracking branch of branch X?" We have branch Dev1 with remote Test, and remote Test with a URL. It doesn't explicitly say "tracking", but that seems to be what it shows. It's common practice to rename and add "origin" when the upstream host changes (move from github to gitlab, say), but that leaves the tracking information and reporting stale. Nov 18, 2021 at 18:22
7
git branch -vv

shows exactly what you ask for. It shows the local branches together with the corresponding remote branch they are tracking.

2

Add these runes to the [alias] section of your .gitconfig file:

show-tracking = !sh -c 'git ls-remote . |grep `git log -1 --grep="git-svn-id" --format=%H`|perl -pe "s/[[:alnum:]]+[[:space:]]//"'
0

I needed to find the corresponding remote branch (if any) for each local branch within a loop that was acting on a list of the local branches. I ended up using the following:

git for-each-ref --format='%(refname:short):%(upstream:short)' refs/heads | grep "^LocalBranchName:.*/" | sed "s/^LocalBranchName://"

This will output nothing (an empty string) for local branches that don't have a corresponding remote branch ("someremote/somebranch").

-1

Try git branch with options:

 -r
List or delete (if used with -d) the remote-tracking branches. 

-a
List both remote-tracking branches and local branches. 

Otherwise, examine your .git/config.

2
  • 4
    They show branches, but I don't understand which one is tracking which.
    – PJ.
    Aug 27, 2010 at 2:42
  • Exactly as PJ said, you have to do git branch -vv to get the connection between the local and remote branches.
    – zkilnbqi
    Mar 13 at 1:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .