Skip to content

Instantly share code, notes, and snippets.

@MattyAyOh
Created April 15, 2022 18:22
Show Gist options
  • Save MattyAyOh/b035372767aca4f96fe0acc694293e50 to your computer and use it in GitHub Desktop.
Save MattyAyOh/b035372767aca4f96fe0acc694293e50 to your computer and use it in GitHub Desktop.
Rebasing w SourceTree (As opposed to merging from parent branch)

The Problem

When your parent branch receives new commits, your branch's parent commit is no longer the latest.

We want to pull those commits onto our working branch

One way is to merge your parent branch into your working branch, which will aggregate the new commits and create a merge commit.

The 2 main problems:

  • conflicts can be difficult to deal with, and oftentimes result in erroneous code being merged onto your working branch
  • you have an extra merge commit on your branch, which contain changes already in commits on the parent branch, which can cause other issues when it comes to merging back to the parent branch.

The Solution is to use git rebase

The concept of rebasing is simple. When you first branched, and made commits, each of your commits are built on top of a parent commit, in the parent branch.
What rebasing is, is taking each of your commits on your working branch, and re-applying them on a new parent commit, which is ideally the latest commit on the parent branch.

This makes it so:

  • You resolve each conflict on a commit-per-commit basis, which are easier to handle because the diff will be contained to one commit (much smaller), and because you can remember the purpose of each commit
  • The number of commits on your working branch remains unchanged

Using a GUI is the easiest, I recommend SourceTree Make sure that in your "File status", you are using "Split view staging"

With SourceTree, make sure that your local repo has the latest commit for your parent branch.

  1. Double-click your parent branch
  2. Pull

Then switch to your working branch

  1. Double-click on your working branch

Then start the rebase,

  1. Right-click your parent branch, selected "Rebase your current changes on PARENT_BRANCH_NAME"

If there are conflicts,

  1. Go to File status, and unstage the files that are conflicting
  2. Resolve your conflicts however you normally do so (I like to manually do it in the IDE)
  3. Once conflicts are resolved, stage the file
  4. In the Main Menu, go to "Actions -> Continue Rebase"
  5. If there are more conflicts, repeat steps above

If there are no conflicts

  1. Then there will be no dialog and you will see that your local branch will indicate that it is "#ahead" and "#behind"

This is because the new rebased commits have new commit hashes, so when your local repository is comparing your local branch to the remote branch,

  • it will see that it is "#ahead" (it sees the new commit hashes in local, which don't exist in remote, and counts those as commits ahead
  • it will see that it is #behind" (it sees that the local branch is missing commit hashes, when comparing to remote, which is really just the old commit hashes before the rebase)

What you do now, is do a git push --force

  1. In SourceTree preferences, go to Advanced, and check "Allow force push"
  2. While you have your working branch checked out, press push, and check "Force push"
  3. Say OK in the next dialog

Now remote will be matching your local working branch, and you have finished rebasing! 🎉

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