Branches and remotes
Merge, squash merge and rebase between branches
7 min read
There are three ways to bring the work from one branch into another:
- Merge: combines the other branch's commits into the current one. When both have moved, git creates a merge commit with two parents.
- Squash merge: brings all the other branch's changes in as a single commit on the current one, without linking the histories.
- Rebase: redoes the current branch's commits on top of the other branch's tip. The history stays linear, but the redone commits get new IDs.
The three actions are in the menu of any branch (local or remote) and apply to the current branch. To combine two branches without checking one out first, drag one onto the other (see Dragging branches).
With main checked out, in the feature/login branch's menu (LOCAL or REMOTE panel, or the label
in the graph):
- Merge feature/login into main
- For a remote branch: Merge origin/feature/login into main
- In a commit's menu, the submenu of each remote branch pointing to it has the same item.
- Check out the branch that will receive the merge (for example,
main). - Right-click the branch that will go in (
feature/login). - Choose Merge feature/login into main.
- If conflict prediction finds a conflict, the Before merging dialog opens (see below).
- The bar shows "Merging feature/login…". Without a conflict, the merge finishes on its own, with git's default message.
git merge --no-edit feature/login
--no-edit accepts git's default message ("Merge branch 'feature/login'") without opening an
editor. If main has no commits that feature/login doesn't have, git just moves main forward
(fast-forward).
Ctrl+Z undoes "Merge feature/login": the current branch goes back to the commit before the merge. With the merge stopped midway by a conflict, Ctrl+Z doesn't undo the merge: to give up, use Abort. Once you finish the stopped merge (with Continue, through the commit box or with Finish the merge without hooks), Ctrl+Z undoes the whole merge, with the conflict resolutions, and takes the branch and the files back to the commit before the merge.
Use it when the work branch's history doesn't matter ("wip" commits, typo fixes) and you want a clean commit on the destination.
- The branch's menu: Squash merge feature/login into main…, right below the regular merge.
- Also in a remote branch's menu and in the remote branches' submenu in the commit menu.
- With
mainchecked out, choose Squash merge feature/login into main…. - The Squash merge feature/login into main dialog explains: "The 4 commits from feature/login go into main as a single commit, with the message below (feature/login's history isn't linked to main's)."
- Review the Summary (it comes filled in with "Squash merge feature/login into main") and the Description (it comes with the list of the commits brought in, one per line, from oldest to newest).
- If you want to skip the commit hooks, check Skip hooks on the commit (--no-verify).
- Click Squash merge.
- If conflict prediction finds a conflict, the Before merging dialog opens.
- At the end, "Squash merged feature/login into main" appears.
| Option | What it does | Default |
|---|---|---|
| Summary | The first line of the commit message. | "Squash merge feature/login into main" |
| Description | The body of the message. | one * title per commit brought in (without the merge commits) |
| Skip hooks on the commit (--no-verify) | Doesn't run the pre-commit and commit-msg hooks on the squash commit. | unchecked |
git merge --squash feature/login
git commit -q -F - # the dialog's message, through standard input
# with the box checked: git commit -q -F - --no-verify
- Nothing to bring in: "Nothing to bring in: everything in feature/login is already in main." No commit is made.
- Conflict: "The squash merge hit conflicts. Resolve the conflicted files and commit: the prepared message is already in the commit field." Resolve them in the conflict editor and commit through the usual commit box.
- Commit rejected (hook, signing…): "The commit wasn't made, but the squash result is staged and the prepared message went to the commit field." Fix the cause and commit.
In those last two cases, the commit box shows "Prepared message from the squash merge of feature/login", with Use and a button to dismiss the message.
Ctrl+Z undoes "Squash merge of feature/login" once the commit is made.
Rebase takes the commits that only the current branch has and reapplies them on top of the other
branch's tip. It's the way to update a work branch with main without a merge commit.
With feature/login checked out, in the main branch's menu:
- Rebase feature/login onto main
- For a remote branch: Rebase feature/login onto origin/main
- To choose commit by commit (reorder, combine, edit): Interactive rebase of feature/login onto main… (see History).
- Check out the branch that will be redone (
feature/login). - In the base branch's menu (
main), choose Rebase feature/login onto main. - If conflict prediction finds a conflict, the Before rebasing dialog opens.
- The bar shows "Rebasing onto main…".
git rebase main
Caution
Rebase changes the IDs of the redone commits. If the branch was already on the remote, the next push will have to be forced (see Force push). Don't rebase a branch other people use without agreeing on it first.
Ctrl+Z undoes "Rebase onto main": the branch goes back to the previous commits. If the rebase stopped at a conflict and you finished it later (with Continue or Skip this commit in rebase until the end, or even from the terminal), the whole rebase goes into Ctrl+Z when it finishes, as if it had never stopped. If aborted, nothing goes in: the branch is already back to what it was.
Before starting, Versora simulates the merge (or the rebase) in memory, without touching your files. If the simulation finds a conflict, the Before merging or Before rebasing dialog opens, with the Conflict predicted block: "Merge feature/login into main is expected to conflict in 2 files:" and the list of files.
- Merge anyway (or Rebase anyway) goes ahead. The merge stops at the conflicts and the conflict editor helps you resolve them; the rebase stops at each commit that conflicts.
- Back gives up without changing anything.
The prediction only takes into account the commits on both sides: uncommitted changes aren't counted. It needs git 2.38 or newer and can be turned off in Preferences › Checks › Conflict prediction › Before merging or rebasing. See Conflict prediction.
If the merge or the rebase stops at a conflict, the notice "The operation stopped because there are conflicts in 2 files." shows up with the guidance: "Click each file in the "Conflicted" section to resolve it in the conflict editor, then click Continue — or Abort to give up."
From the palette (Ctrl+P) there are also "Continue merge", "Abort merge" and, in a rebase, "Skip this commit in rebase". The conflict editor is in Conflicts.
While the operation is stopped, Ctrl+Z undoes the resolution steps (saving a file in the conflict editor, using one of the versions, staging a conflicted file, a discard), one at a time. When the operation finishes, those steps leave the undo history and the whole operation goes in: the next Ctrl+Z goes back to the state before the merge or the rebase. See Stopped merge, rebase, cherry-pick and revert.
- A
pre-merge-commitorcommit-msghook that rejects the merge commit leaves the merge in progress, with everything staged. The notice says "The pre-merge-commit hook refused the merge commit. The merge is still in progress, with everything staged." and offers Finish the merge without hooks. - A
pre-rebasehook that rejects the rebase makes the notice offer Try without hooks.
See Skip hooks.
- "You're on a detached HEAD: check out the branch that will receive the squash merge.": the squash merge needs a branch checked out.
- "The commit message is empty.": the squash merge summary can't be left blank.
- The merge and rebase items don't show up with a detached HEAD: check out a branch first.
- Preferences › Checks › Conflict prediction: Warn when it would conflict (before merging or rebasing) and Predict conflicts in the background (marks branches with ⚠).
- To see beforehand what a merge would bring in, use What enters main when merging feature/login in the branch's menu (see Inspection).