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".
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".
Using the example of my copy of Puppet checked out from the upstream Git repository on Github.com...
Then if I were to execute the following:
And finally re-run the
git remote show origin
command again I will then see the following down near the bottom:For all branches:
For local branches only:
For remote branches only:
shows you all branches as well as the name of the upstream branch.
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.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 withgit config branch.<name>.remote
andgit config branch.<name>.merge
(probably usinggit for-each-ref
to build the commands for each local branch name).For a particular branch, you can use
git rev-parse
with the@{u}
or@{upstream}
suffix on the branch name, e.g.:... or for the abbreviated form, add
--abbrev-ref
You can generally use the
branch@{upstream}
syntax wherever a commit is expected.I use the following shell script (named
git-tracks
) to show the remote branch that is tracked by the current branch: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..git/config
file also will provide the tracking branch info asshows exactly what you ask for. It shows the local branches together with the corresponding remote branch they are tracking.
Add these runes to the
[alias]
section of your .gitconfig file: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:
This will output nothing (an empty string) for local branches that don't have a corresponding remote branch ("someremote/somebranch").
Try
git branch
with options:Otherwise, examine your
.git/config
.