Skip to content

Instantly share code, notes, and snippets.

@karenc
Last active February 11, 2021 22:59
Show Gist options
  • Save karenc/ad73ff9720fc6a2eafc3b36038f2b662 to your computer and use it in GitHub Desktop.
Save karenc/ad73ff9720fc6a2eafc3b36038f2b662 to your computer and use it in GitHub Desktop.

Git rebase Example

You have a branch based on origin/develop (commit 300). It has 2 commits My commit 1 and My commit 2. origin/develop has a new commit commit 301. Now you need to rebase your branch so your commits are on top of the current origin/develop.

git-rebase-branch

Tips:

  1. You need to know which commits are in your branch: My commit 1 and My commit 2.
  2. Use git rebase -i origin/develop (interactive) and pick only the commits in your branch.
  3. Every one of the commit diff should look almost the same as ones before you rebase (assuming there is no reimplementation).
  4. Since the commit hashes changed (the history of your branch), you will need to force push to origin.
  5. Instead of maintaining a local develop branch, just use origin/develop unless you are working on develop.
  6. Rebase replays your commits on top of origin/develop, which means you have to resolve conflicts for each of your commits. It is therefore helpful to squash wip commits beforehand.

Steps:

  1. Fetch updates from origin:

    git fetch -p
    
    git log --graph --oneline --decorate origin/develop HEAD
    
    * a6b4235 (HEAD -> my-branch) My commit 2
    * 99b0f26 My commit 1
    | * b44e301 (origin/develop) commit 301
    |/
    *   a407a19 commit 300
    * eefda89 commit 299
    * cbea031 commit 298
    
  2. Use interactive rebase to pick commits: (Remove any lines that contain commits that are not yours)

    git rebase -i origin/develop
    

    This opens an editor with something like this:

    pick a407a19 commit 300
    pick 99b0f26 My commit 1
    pick a6b4235 My commit 2
    

    Make sure to remove the line pick a407a19 commit 300, it is not one of your commits so should not be in your branch:

    pick 99b0f26 My commit 1
    pick a6b4235 My commit 2
    

    There are other commands like "edit", "fixup", "squash" etc. Try them out too!

    Save the file and quit

  3. Resolve any conflicts and continue the rebase:

    git add file-with-conflicts.txt
    
    git rebase --continue
    
  4. Check that your commits still make sense:

    git log --graph --oneline --decorate origin/develop HEAD
    

    It should look something like:

    * 0f0b7da (HEAD -> my-branch) My commit 2
    * 74ff1b8 My commit 1
    * b44e301 (origin/develop) commit 301
    * 9727723 commit 300
    * eefda89 commit 299
    * cbea031 commit 298
    

    To check the changes in each commit:

    git show --reverse origin/develop..
    

    To check all changes compared to the branch on origin:

    git diff origin/my-branch..
    
  5. Force push to origin:

    git push -f origin HEAD
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment