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 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"
![](https://camo.githubusercontent.com/0086f2d1a31ed8906e4ee86ad1ed5d9c796051477249e2d07e992a06430b6cb0/68747470733a2f2f692e696d6775722e636f6d2f505836614c49592e706e67)
- Double-click your parent branch
- Pull
- Double-click on your working branch
- Right-click your parent branch, selected "Rebase your current changes on PARENT_BRANCH_NAME"
- Go to File status, and unstage the files that are conflicting
- Resolve your conflicts however you normally do so (I like to manually do it in the IDE)
- Once conflicts are resolved, stage the file
- In the Main Menu, go to "Actions -> Continue Rebase"
- If there are more conflicts, repeat steps above
- 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)
- In SourceTree preferences, go to Advanced, and check "Allow force push"
- While you have your working branch checked out, press push, and check "Force push"
- Say OK in the next dialog
Now remote will be matching your local working branch, and you have finished rebasing! 🎉