This Git repository gives you an example where you can train/experiment with Git's rebasing feature.
You can train two things here:
- Rebase branch
rebase/feature/multiply
ontorebase/master
. - Rebase and squash branch
rebase/feature/multiply
ontorebase/master
(using interactive rebasing).
In both cases, there will (should) be a merge conflict that you need to resolve.
Follow these instructions to get the repository into a state where you can train:
$ cd <an-empty-dir>
$ git clone -b rebase/master https://github.com/skrysmanski/rebase-training.git .
$ git checkout --track origin/rebase/feature/multiply
Note: If something went wrong, I recommend that you delete the whole directory and re-clone the repository before you try again.
- Open the "Log / History" tab
- Checkout source branch (
rebase/feature/multiply
) - Right-click head of target branch (
rebase/master
) and select "Rebase..." - Resolve conflict via the "File Status" tab (Right-click on
Program.cs
and chose "Resolve Conflict") - In the menu choose "Action" -> "Continue Rebase"
Interactive rebasing allows you to squash, delete and reorder commits before rebasing them onto a different branch.
Unfortunately, Sourcetree contains an ancient bug; so this feature doesn't fully work.
My current workaround: First do a regular rebase, then do the interactive rebase.
- Rebase source branch (
rebase/feature/multiply
) on target branch (rebase/master
) - Open the "Log / History" tab
- Right-click the parent of oldest commit you want to rebase interactively (here
5c3d0ea
) and select "Rebase children of xxx interactively..." - Modify the commits as desired (check out this video for all the options)
Once you're done you (probably) need to push your changes back to origin
. Normally Git doesn't allow you to push these kind of changes.
Instead you need to "force push" them; ideally by using --force-with-lease
instead of --force
. You can read on the differences between those two in this article.
To be able to do a "force push" with Sourcetree, this needs to be enabled in the options.
In the menu, go to "Tools" -> "Options" -> "Git" and check "Enable Force Push". Make sure to also check "Use Safe Force Push".
Then you can select "Force Push" in "Push" dialog. (No matter the name, this will automatically use --force-with-lease
instead of --force
when "Use Safe Force Push" is enabled in the options.)