1

When you have upstream that is different from origin, how you can count difference between local and upstream?

git rev-list --count upstream/master..master returns 0 despite the fact that upstream is few commits ahead of local master branch.

user268587
  • 19
  • 1
  • 1
    `git rev-list` works for me. Could you show the output of `git branch -a -v`? (or even better, an screenshot of `gitk --all`? – Daniel Apr 04 '20 at 18:03
  • @MondKin `gitk --all` output: * master remotes/origin/HEAD remotes/origin/master remotes/upstream/master – user268587 Apr 04 '20 at 18:19

2 Answers2

2

Consider using the three-dot notation and the --left-right flag:

git rev-list --left-right --count upstream/master...master

As described in the gitrevisions documentation, the three-dot notation A...B produces a symmetric difference of the sets of commits reachable from the two identifiers or hash IDs A and B. (For much more on reachability, see Think Like (a) Git.)

Normally this would just be:

  • all commits on upstream/master that aren't on master, plus
  • all commits on master that aren't on upstream/master

which would just be a sum. But adding --left-right tells Git to split the sets into two. Without --count, you get a listing annotated with < and > markers to tell you which commit hash goes with which identifier. With --count, you get two counts. In this case, if upstream/master is strictly ahead of master, the first count will be nonzero and the second count will be zero. For instance, if upstream/master is 2 ahead, the output is 2 0.

This works if you reverse the names, too; it's just that now the counts are swapped around. For the above case, you'd now get 0 2.

Pick whichever order you like and get both counts, or use the two-dot notation to get just one count. The one count you get, with A..B, is the count of commits reachable from B but not reachable from A. If upstream/master is "ahead" and you want that count, you need master..upstream/master when using the two-dot method.

torek
  • 448,244
  • 59
  • 642
  • 775
1

The number of commits upstream is ahead of master:

git rev-list --count master..upstream/master

master..upstream/master is the range of commits that are in upstream but not in master. It's equivalent to upstream/master ^master.

See What are the differences between double-dot ".." and triple-dot "..." in Git commit ranges? for more information.

krisz
  • 2,686
  • 2
  • 11
  • 18