2

When I type git status I see
Your branch is up-to-date with 'origin/master'.

What if I have more than one remote, and I want to compare with a remote call 'feature', I type git status it still compare with origin. Is there a way that when I type git status I see
Your branch is up-to-date with 'feature/master'.?

Also is it possible to do something like git status feature -> compare local master with feature/master, or git status feature1 feature2 -> compare local master with feature1/master and feature2/master?

If not possible, what is the best way to see how many commits ahead/behind with certain remotes or branches(even with two different local branches)? I don't want git diff because that gives me too many details

Thanks in advance.

stevemao
  • 1,423
  • 1
  • 16
  • 29
  • You need to change its upstream. Possible duplicate: http://stackoverflow.com/questions/520650/make-an-existing-git-branch-track-a-remote-branch – fpietka Aug 06 '14 at 09:01
  • I think this question will guide you to an answer : [Show git ahead and behind info for all branches, including remotes](http://stackoverflow.com/questions/7773939/show-git-ahead-and-behind-info-for-all-branches-including-remotes) – LeGEC Aug 06 '14 at 13:08
  • @fpietka I don't think my question is related to upstream. And the one you posted is a totally different problem. – stevemao Aug 06 '14 at 13:09
  • Thanks @LeGEC, my original thought was to use an easy command to compare (behind/ahead) any branch (doesn't matter if they are local or remote, or maybe even branches of different remotes). Of course I can always use `git diff` to see more details but sometimes I just want an rough idea of how much difference between branches. `git status` does a really good job but it only compares local and it's upstream. The answer in the link is really close to what I want, but it looks rather complicated and it can't compare any two given branches. – stevemao Aug 06 '14 at 13:27

1 Answers1

2

Loosely based on Show git ahead and behind info for all branches, including remotes, here is a shell script which should allow you to have a "ahead ... behind ..." message for any two commits :

file ./ahead.sh :
#!/bin/bash
left=$1
right=$2

leftahead=`git rev-list --count $right..$left`
rightahead=`git rev-list --count $left..$right`

echo "$left (ahead $leftahead) | (behind $rightahead) $right"

usage :

$ ./ahead.sh HEAD origin/master
HEAD (ahead 7) | (behind 0) origin/master

git rev-list $left..$right displays the sequnce of commits starting from (but not including) the common ancestor for $left and $right up to $right.

git rev-list --count $left..$right will simply count the number of commits in this sequence.

Using both git rev-list --count $left..$right and git rev-list --count $right..$left, you get the ahead/behind counts (what is "ahead" and "behind" depends on which commit serves as reference).


For a list of modified files (à la git status), you can use git diff --name-status :

#!/bin/bash
left=$1
right=$2

leftahead=`git rev-list --count $right..$left`
rightahead=`git rev-list --count $left..$right`

echo "$left (ahead $leftahead) | (behind $rightahead) $right"
git diff --name-status $right $left

update

If git diff --name-status produces too long an output, the pager will kick in, and hide the initial ahead | behind line. Here is a fix, using the --no-pager option :

#!/bin/bash

function displayStatus {
  left=$1
  right=$2

  leftahead=`git rev-list --count $right..$left`
  rightahead=`git rev-list --count $left..$right`

  echo "$left (ahead $leftahead) | (behind $rightahead) $right"
  git --no-pager diff --name-status $right $left
}

displayStatus $1 $2 | less
Community
  • 1
  • 1
LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • This is awesome! Could you please add some comments to explain what the script does? Thanks so much! – stevemao Aug 06 '14 at 13:39