What is rebase?
Imagine you and a co-worker are in charge of tickets A and B for a new feature. Let’s say it is on the
feature branch. Your co-worker completed the ticket before you do and this is how the branch looks like when you are ready to submit your PR:
In your local history, your work has the index(or SHA) of 2 but on the
feature branch 2 is B.
We want to maintain a linear commit history and avoid as many merge conflicts as we can. In other words, we want to achieve something like this:
We do that with rebase.
How to rebase(with SourceTree)
- Make sure your local copy of
featureis up to date. Do a fetch and pull.
- Check out your ticket A branch. Right-click on the local
featurebranch and select “rebase onto
- Now if you do a
git status, you should see something like “10↓1↑”. The 10 is how many changes you are behind the
featurebranch and the 1 commit you have is to modify your SHA from 2 to 3.
- SourceTree doesn’t allow a force push so in your terminal, do
git push -f.
And that’s it! You placed your code nicely in git.