Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a branch be both X commits ahead and Y commits behind?

Tags:

git

github

I was checking some of the projects in github and in one of them I noticed this:

This branch is 287 commits ahead, 361 commits behind X:Master

How exactly is this possible ?

like image 452
Cemre Mengü Avatar asked Oct 21 '25 03:10

Cemre Mengü


1 Answers

Just like that:

       o ----- ... 285 commits ... -- o     <-- master
      /
o -- o -- o -- ... 359 commits ... -- o     <-- X/master

You created 287 commits on branch master in your local repository. These commits are not accessible from X/master. This is why master is "287 commits ahead X/master".

In the same time, other developers created 361 new commits on X/master. Since the local master and X/master diverged, these 361 commits are not accessible from master and that's why master is "361 commits behind X/master".

This is a normal situation in a versioning control system. You can synchronize your master branch with X/master in two ways:

  1. merge

    Run:

    git checkout master
    git merge X/master
    

    This creates a new commit that contains all the changes from both master and X/master and has two parents: the current tips of the master and X/master branches.

    If it succeeds, the graph looks like this:

                                       +--- old "master"  
                                       v
           o -- ... 285 commits ... -- o -- o   <-- master (the merge commit)
          /                                /
    o -- o -- o -- ... 359 commits ... -- o     <-- X/master
    
  2. rebase

    Run:

    git checkout master
    git rebase X/master
    

    This moves all the commits that are on master but not on X/master (the 287 commits that are "ahead") on the X/master branch, after the commits 361 that are on X/master and are not accesible from master.

    If it succeeds, the graph will look like this:

         +--- old split point
         v
    o -- o - ... 360 commits ... - o - ... 286 commits ... - o   <-- master
                                   ^
                                   +---- X/master
    
like image 58
axiac Avatar answered Oct 23 '25 18:10

axiac