Show git ahead and behind info for all branches, including remotes
On a github project you can go to a /branches page and se pretty graphs like this one that for each branch show how far behind and how far ahead each branch is with respect to master.
Is there a command line tool that does something similar? Something that works with remotes as well? For example,
git branch -v -v
is close to what I am looking for, but only works for local branches.
I've been curious about this as well, so i just whipped up a git branch-status
script that gives this information using git for-each-ref
#!/bin/bash
# by http://github.com/jehiah
# this prints out some branch status (similar to the '... ahead' info you get from git status)
# example:
# $ git branch-status
# dns_check (ahead 1) | (behind 112) origin/master
# master (ahead 2) | (behind 0) origin/master
git for-each-ref --format="%(refname:short) %(upstream:short)" refs/heads |
while read local remote
do
[ -z "$remote" ] && continue
git rev-list --left-right ${local}...${remote} -- 2>/dev/null >/tmp/git_upstream_status_delta || continue
LEFT_AHEAD=$(grep -c '^<' /tmp/git_upstream_status_delta)
RIGHT_AHEAD=$(grep -c '^>' /tmp/git_upstream_status_delta)
echo "$local (ahead $LEFT_AHEAD) | (behind $RIGHT_AHEAD) $remote"
done
Usage:
$ git branch-status
dns_check (ahead 1) | (behind 112) origin/master
master (ahead 2) | (behind 0) origin/master
Update 2015
My initial answer below is not ideal as the upstream branch is not necessarily the branch you are pushing to. It is only the branch you are pulling from.
With Git 2.5+, the correct command is:
git for-each-ref --format="%(refname:short) %(upstream:track)" refs/heads
See more at "Viewing Unpushed Git Commits".
(as pointed out by void.pointer in the comments, upstream:track
is more precise than push:track
, depending on the default push policy)
Git 2.13 (Q2 2017) uses a more generic ref-filter API with a more complete git for-each-ref push
:
See commit 3d9e4ce, commit 949af06, commit 56b4360, commit 6eac70f, commit 1a34728, commit 1a0ca5e, commit 3a42980, commit 17938f1, commit 3ba308c, commit a798410, commit b180e6f, commit 01f9582, commit 7743fcc, commit ffd921d, commit 99c6a71, commit d4919bb, commit 42d0eb0, commit 4f3e3b3, commit c58fc85 (10 Jan 2017) by Karthik Nayak ( KarthikNayak
).
(Merged by Junio C Hamano -- gitster
-- in commit 93e8cd8, 27 Feb 2017)
push:
The name of a local ref which represents the @{push}
location for the displayed ref.
Respects :short
, :lstrip
, :rstrip
, :track
, and :trackshort
options as upstream
does.
Produces an empty string if no @{push}
ref is configured.
If lstrip=<N>
( rstrip=<N>
) is appended, strips <N>
slash-separated path components from the front (back) of the refname
(eg %(refname:lstrip=2)
turns refs/tags/foo
into foo
and %(refname:rstrip=2)
turns refs/tags/foo
into refs
).
If <N>
is a negative number, strip as many path components as necessary from the specified end to leave -<N>
path components
(eg %(refname:lstrip=-2)
turns refs/tags/foo
into tags/foo
and %(refname:rstrip=-1)
turns refs/tags/foo
into refs
)
Original answer (2014)
Another way will be available with Git 1.9/2/0 (Q1 2014).
See commit b28061c from Ramkumar Ramachandra (artagnon):
for-each-ref
: introduce %(upstream:track[short])
Introduce:
%(upstream:track)
to display " [ahead M, behind N]
" and %(upstream:trackshort)
to display " =
", " >
", " <
", or " <>
" appropriately (inspired by contrib/completion/git-prompt.sh
). Now you can use the following format in for-each-ref:
%(refname:short) %(upstream:trackshort)
to display refs with terse tracking information.
Note that :track
and :trackshort
only work with " upstream
", and error out when used with anything else.
For this type of issues,you can use "Gitk"`
sudo apt-get update
sudo apt-get install gitk
This can help you alot.You can even see git ahead for all branches
链接地址: http://www.djcxy.com/p/31754.html