Merging & Rebasing
Integrating changes from one branch into another can be done via merge or rebase.
Before You Integrate...
Before starting a merge or rebase, you should check a couple of things:
- Your working copy should be clean. In case any local changes are present, use Git's Stash to temporarily save them away.
- Check out the branch that should receive the changes. The other branch will not be modified by the operation in any way.
Merging Branches
The most common method to integrate one branch into another is to perform a „merge“. To merge changes from another branch into your current HEAD, you can do one of the following:
- click the button in the toolbar
- drag the branch in the sidebar and drop it onto your current HEAD branch item
Rebasing Branches
When doing a rebase, Git takes all the commits from one branch and reapplies them (like patches) on the other branch one after the other. Thereby, history is rewritten: It will appear as if the commits had been made consecutively on the same branch, not on a different branch. No merge commit will be created, but commits will have new SHA1-IDs.
As a golden rule you should never rebase commits that have already been published on a remote repository (pushed).
To rebase your HEAD branch onto another branch, you can do one of the following:
- click the button in the toolbar
- drag the branch in the sidebar and, while holding down the OPT key, drop it onto your current HEAD branch item
Using the --onto
Option for Rebase
The "Rebase" command is well known in Git, but not its --onto option, which can be immensely helpful. In short, rebase --onto allows you to "switch the base" for a series of commits.
A common, practical use case for this is when you want to switch the base of a feature branch to a different branch:
A normal rebase wouldn't work for this scenario. rebase --onto allows you to be much more precise.
For maximum flexibility, Tower also offers to use the rebase onto option with specific commits. Simply select a range of commits, right-click and choose the “Rebase X revisions onto…” action. You will be prompted with a dialogue where you can select the revision to rebase onto.
Tip: Learn more about merge & rebase in our learn section.