0

On the current branch, we can run this command:

git checkout

to obtain a useful message informing us, "Your branch is ahead of upstream by N commits."

How can an analogous message be obtained for a different branch?

(Needless to say, without switching to that branch first?)

(Also, without doing some hacky text filtering, like generating a log of the local commits, counting them with wc and then faking out the message; that is, is there a direct way inside the git executable?)

Fictitious example:

$ git <magic-incantation> foo-branch
Branch foo-branch is ahead of origin/foo-branch by 17 commits.

What git arguments constitute <magic-incantation>?

Kaz
  • 55,781
  • 9
  • 100
  • 149
  • Take a look at this question: http://stackoverflow.com/questions/7773939/show-git-ahead-and-behind-info-for-all-branches-including-remotes – merlin2011 May 01 '14 at 20:09
  • @merlin2011 The accepted answer there qualifies as hacky unix scripting. I see `/dev/null` and `grep -c` crap in there. – Kaz May 01 '14 at 20:20
  • You can create a git alias that temporarily switches your checkout to the branch, then runs `git status`, then checks out your previous branch. Is that the kind of thing you're looking for? –  May 01 '14 at 22:06
  • 1
    @Cupcake if it will preserve the timestamps on all the files so nothing gets recompiled as a result that could be okay, but I'm looking for the tree not to be disturbed at all. – Kaz May 01 '14 at 22:34
  • I know you said you wanted to use something already built into git to do this, but this is really trivial: `git log --oneline @{u}.. | wc -l`, or `git log --oneline origin/.. | wc -l`. You could even turn ***that*** into a parameterized git alias, if you really wanted to. –  May 01 '14 at 23:08
  • @Cupcake my question specifically rules out that exact solution, see the wording: "generating a log of the local commits, counting them with wc". – Kaz May 01 '14 at 23:37
  • @Kaz I know, I read that, I'm just pointing out (unnecessarily?) that it's a really easy, simple thing to do `:/` –  May 01 '14 at 23:38

1 Answers1

3

Use git branch -v

In git version 1.9.0, you can run the following:

$ git branch -v
  master 5f95c9f [behind 167] Git 1.9.0
* next   70b5e56 [behind 20] Merge branch...

or more verbose:

$ git branch -vv
  master 5f95c9f [origin/master: behind 167] Git 1.9.0
* next   70b5e56 [origin/next: behind 20] Merge branch...next

Without any additional flags like -a or -r, the output will be for each of your local branches, with the currently checked-out branch prefixed with an asterisk *. Note that the local branches have to be tracking a remote branch, which is used as their reference.

I think the output of -v and -vv was actually introduced in earlier versions of git, but off the top of my head I don't remember which versions.

Documentation

From the official Linux Kernel documentation for git branch

-v
-vv
--verbose

When in list mode, show sha1 and commit subject line for each head, along with relationship to upstream branch (if any). If given twice, print the name of the upstream branch, as well (see also git remote show <remote>).