Search code examples
gitgit-mergegit-branchgit-rebase

Git rebase vs merge after rebase master on feature branch


I'm learning about rebase and saw a workflow of doing a rebase on a feature branch and then merging it into master (as an example):

git checkout feature
git rebase master
git checkout master
git merge feature

However I saw another similar option of the last command being (instead of 'git merge feature'):

git rebase feature

How does the last part differ in this instance? Won't the result be the same linear timeline of the feature commits on top of master?


Solution

  • You may be learning about rebase, but I don't think you've quite understood it yet.

    The command git rebase xxx means: rebase the branch I'm on now, onto xxx. It does not change xxx in any way.

    This is confusing, because git merge xxx means exactly the opposite: it means, merge xxx into the branch I'm on now.

    So for example if feature split off from master, and master had some commits and feature had some commits, then git switch feature; git rebase master changes the split-off point. That's effectively all it does.

    If we start with this:

    A -- B -- C -- D -- E (master)
               \
                X -- Y -- Z (feature)
    

    Then git switch feature; git rebase master results in this:

    A -- B -- C -- D -- E (master)
                         \
                          X' -- Y' -- Z' (feature)
    

    (Note the use of the "prime" marker on the moved commits, because you cannot actually move a commit; these commits are copies of the original X, Y, and Z.)