Search code examples
gitgithubrebasing

What happens if I try to rebase a commit that is already pushed to a development branch


The use case is following

I pull from the development branch then I make some code changes and push then

After few days I come back and do to a git status, this shows that I am ahead of the master branch by 1 commit, obviously I haven't pulled from the master for a couple of days

Now before I make my changes I decide to do a git pull with rebase , git pull --rebase origin master

The question is: will the commit that I had in my local repo branch go on top of the master or since I had already pushed it I will simply get to the tip of the global master/remote branch?

I am new to concept of rebasing so please help explain this.


Solution

  • Let's consider the following.

              D---E---F origin/master
             /
    A---B---C---X master
            ^
            origin/master in your repository
    

    A few days ago your local master and origin/master were both in sync and each had commits A, B and C. At some point you committed X in your local master. And concurrently others pushed commits D, E and F to origin/master.

    At this point if you run git pull --rebase origin master, will pull all commits from the origin/master as is and the commit X will be replayed over top of F and a new commit id will be generated X'.

    The new commit graph will look like:

                         origin/master
                        /
    A---B---C---D---E---F---X' master
                        ^
                        origin/master in your repository
    

    So yes, the old commit X will be rebased at the top of your local master as commit X'.

    EDIT:

    the question in this use case what if I had pushed X and then D,E,F were made, how will my local branch look asfter i execute the pull with rebase now?

    In this case, after pushing your commit X, both your local repository and origin/master will be in sync.

    
                 origin/master
                /
    A---B---C---X master
                ^
                origin/master in your repository
    
    

    After few days the commits D, E and F are pushed by others to the origin/master. The commit graph will then look like this:

                 D---E---F origin/master
                /
    A---B---C---X master
                ^
                origin/master in your repository
    

    Now if you run git pull --rebase origin master it will give identical results to the git pull origin master command, because there is nothing to rebase from. You do not have have any commits ahead of origin/master. At the end of this command your graph will look like this:

                              origin/master
                             /
    A---B---C---X---D---E---F master
                            ^
                            origin/master in your repository
    

    Since your commit X was already in origin/master, it will not be replayed on top of D, E and F.