2

I have two branches: origin/A and A (locally).

Assuming one - and only one - has advanced by one commit, but I don't know which branch advanced, how do I tell which one has the extra commit?

IanPudney
  • 5,531
  • 1
  • 20
  • 37
Snowcrash
  • 66,400
  • 60
  • 203
  • 323
  • If its always going to be 2 branches, the method specified by Makato would suffice. Else, I suggest you look at http://stackoverflow.com/a/5972362/4433695 – KayDK May 05 '15 at 19:18
  • possible duplicate of [git: programmatically know by how much the branch is ahead/behind a remote branch](http://stackoverflow.com/questions/2969214/git-programmatically-know-by-how-much-the-branch-is-ahead-behind-a-remote-branc) – Andrew C May 05 '15 at 20:10

3 Answers3

2

Use git fetch to get the latest commits from the tip of your remote, then use git status to tell whether or not your remote or local is ahead.

If origin/A is ahead, then when you run git status, you'll be told that your local A branch is behind origin by a commit.

If your local A is ahead, then when you run git status, you'll be told that your local A branch is ahead of origin by a commit.

Makoto
  • 96,408
  • 24
  • 164
  • 210
1

git rev-list HEAD --count will give you the number of commits on your current branch. Assuming you using github you can see the number of commits on the stop left of the repo.

Joel
  • 4,289
  • 1
  • 24
  • 40
0

If your goal is to do this as part of an automated bash script, you can use the git rev-parse command. To see the commit hash of a given branch, use this command:

git rev-parse branchname

Or, to see the hash of the previous commit from a given branch, use this command:

git rev-parse branchname^

So, if you want to see which branch is ahead, you can run the following four commands and compare output:

git rev-parse A
git rev-parse origin/A^

git rev-parse A^
git rev-parse origin/A

If the output of the first and second commands match, then A is the branch with one additional commit. If the output of the third and fourth commands match, then origin/A is the branch with one additional commit.

If neither pair of commands has matching output, then you don't have one branch that is ahead of the other by exactly one commit.

IanPudney
  • 5,531
  • 1
  • 20
  • 37
  • I'm not sure that guarantees that there is only ever one commit discrepancy between the two branches. It guarantees that there's a SHA difference, but not *necessarily* that there's a numeric difference between the number of commits on these branches. – Makoto May 05 '15 at 19:23
  • You are right, this method only works if the assumption stated in the question are correct: *Assuming one - and only one - has advanced by one commit*. If that assumption is not correct, then *neither* of the command pairs I listed will compare correctly. However, if that assumption is correct, and the branches do differ by exactly one commit, then this method will work. – IanPudney May 05 '15 at 19:26
  • I'm not sure answering a terrible question with an answer very specific to the arbitrary nature of that question is a good thing. – Andrew C May 05 '15 at 20:19