Search code examples
gitatlassian-sourcetreesourcetree

SourceTree - Git Rebase resulting in automatic merge


I'm using SourceTree 1.9.10.0 on Windows 10 and I wan't to rebase my feature branch EUR-1058 onto master. I do it like this:

enter image description here

This results in the following tree:

enter image description here

And If I do a pull an automatic merge happens:

enter image description here

Why is this happening? Is it due to the fact that the commit "Mailcheck rewrite" was on my laptop and I'm now rebaseing from my desktop and the SHA-1 hashes has somehow changed or something similar?


Solution

  • When you rebased your EUR-1058 branch on master, you rewrote the history of that branch. Consider the following diagram illustrating the point:

    master:   ...A -- B ---- C -- D
                       \
    EUR-1058:           E -- F
    

    Rebasing EUR-1058 on master means doing the following steps:

    • rolling the branch back to the commit from which it branched off master, in this case commit B
    • then replaying the commits from master on top of B, in this case replaying commits C and D
    • finally, reapplying the commits you made to EUR-1058 since commit B, i.e. commits E and F


    After rebasing, the diagram looks like this:

    master:   ...A -- B ---- C -- D
                                   \
    EUR-1058:                       E' -- F'
    

    Note that I have labelled E' and F', to indicate that these are in fact new commits, which were made during the rebase.

    Now to answer your actual question, when you tried to do a git pull on EUR-1058, Git first checked to see if your local branch could be fast-forwarded by what is on the remote (or in origin/EUR-1058). In this case, no fast-forward was possible. Git views the remote branch at this point as being totally different than what you have locally. Hence, it completed the pull by making a merge commit.

    What you probably should have done is to force push your rebased EUR-1058 branch to the remote after completing the rebase via:

    git push --force EUR-1058
    

    Using force is necessary here because the history/base of the branch has changed. This is the same reason why you ended up with a merge commit. After the force push, you can continue life as normal.

    As a word of caution, you should realize that rewriting the history of EUR-1058 can cause problems for anyone sharing this branch. So, it should be done carefully.