Branch is ahead of origin by more than one commit after merge

Viewed 932

I had two branches that looked like this:

    X--X [origin/master, master]
.../ 
   \         
    ----X--X--X--X--X--X [origin/x, x]

then I merged x into master so now I have this:

       [origin/master]
    X--X------------------X [master]
.../                     /
   \                    /
    ----X--X--X--X--X--X [origin/x, x]

Now when I git status I get:

On branch master
Your branch is ahead of 'origin/master' by 7 commits.
  (use "git push" ...)
...

Why is it 7 commits? From the picture (which I drew looking at git log --graph) it seems it should be ahead by 1 commit. Other 6 commits are for branch x.

1 Answers

Other 6 commits are for branch x

Aha, that's just where you're wrong. Remember, a branch in Git is not a chain of commits, the way you are thinking of it. It is one commit. Everything else is just commits that are reachable from that one commit, by walking backwards along the parent chain.

Well, a merge commit, such as you have just created, has two parents, one of which is the branch commit that just got merged in. So after the merge, all the commits that were previously reachable only from x are now also reachable from master (because x is now one of the parents of master, which is the merge commit).

So all Git is doing here is counting how many commits are now reachable from master that are not reachable from origin/master, like this:

       [origin/master]    7
    X--X------------------X [master]
.../                     /
   \                    /
    ----X--X--X--X--X--X [origin/x, x]
        1  2  3  4  5  6

Git doesn't care about the topology that makes that be so; it's just reporting a number.

So the number reported by Git might seem unintuitive, but in fact it is perfectly accurate. And it's a useful number! Git is rightly reporting that if you were now to push, causing origin/master to move up to master, origin/master would then be able to reach 7 commits that it presently cannot reach, and the remote repository would acquire 7 commits that it currently does not have.

Related