Skip to content

Instantly share code, notes, and snippets.

@mjkrause
Last active February 14, 2019 15:44
Show Gist options
  • Save mjkrause/6e07b4a8fb23ac2dd6dd669909294b6e to your computer and use it in GitHub Desktop.
Save mjkrause/6e07b4a8fb23ac2dd6dd669909294b6e to your computer and use it in GitHub Desktop.

Simple Rebase

The situation (1), the problem (2, 3) and the solution (4)

  1. Originally, you branched off of develop:
a -- b                           develop
       \
        f -- g                   your_branch
  1. Next, someone else branched off of develop, and merged branch1 back into develop before you did. Now develop is in a different state compared to you first branched off of it.
a -- b -- c        d             develop
      \    \      /
       \    d -- e               branch1
        \
         f -- g                  your_branch
  1. In order to have the features contained in branch1 in your feature branch you need to rebase on develop.
a -- b -- c        d             develop
           \      /
            d -- e               branch1
  1. The solution is rebasing your changes onto the new state of develop. This is done by taking off your changes, fetching the latest state of develop, and then replaying your changes back onto that new state of develop - done.
a -- b -- c        d             develop
           \      / \ 
            d -- e   \           branch1
                      \
                       f -- g    your_branch

First, checkout out develop and make sure that your version (local) is the same as the remote (origin) version. Run git status and look for the message your_branch is up-to-date with origin/your_branch. If not, make it so by

git reset --hard origin/develop

Note that this is going delete all changes you may have (accidentally?) added to branch develop. But if that's not a problem you can just go ahead. Now change back to your branch and start the rebase process by

git rebase develop

This hopefully will have no conflicts. If so, run again git status. That likely returns Your your_branch and 'origin/your_branch' have diverged, and have x and y different commits each, respectively. That's a problem. Now you need to re-write history by forcing a push.

git push --force-with-lease

and run git status again. Confirm that the local and remote version of that branch do not diverge anymore, et voila.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment