History
Squash: combining commits and local squash merge
7 min read
"Squash" means combining several commits into one. In Versora it shows up in two ways:
- Combining commits of the branch itself (rewrites): the chosen commits become one commit, with the message you write.
- Squash merge of another branch (adds): all the changes of a branch go into the current one as a single new commit, without linking the histories.
| You want to… | Use |
|---|---|
| Combine two or more neighboring commits you selected in the graph | Squash N commits… (on this page) |
| Combine a commit with the previous one, straight from the commit menu | Squash into the previous commit…, from commit surgery |
| Combine commits that aren't side by side, or along with other changes (reorder, drop) | The Squash into previous and Fixup (discard message) actions of the interactive rebase |
Fold the fixup! commits into their target commits | Apply pending fixups, from commit surgery |
| Bring a whole branch into the current one as a single commit | Squash merge X into Y… (on this page) |
| Fold a stack branch into the branch below | The fold of the branch stack |
Combines neighboring commits on the current branch into one. The changes of all of them end up in one commit, with the message you write, and the commits above are reapplied on top.
- Select the commits in the graph with Ctrl+click or Shift+click and open the selection menu › Squash 3 commits… (the number follows the selection). See Multiple selection.
- In the graph, click
a1b2c3d Add login screenand Shift+clickc3d4e5f Adjust login screen layout, selecting the three commits between them. - Right-click the selection and choose Squash 3 commits….
- The "Squash 3 commits" dialog explains: "The 3 commits become one, with all of their changes and the message below." and shows the rewrite warning.
- The Summary and the Description start with the messages of all three, from oldest to newest, separated by a blank line, as git would do it. Adjust the text.
- Click Squash commits. While it runs: "Squashing 3 commits…". At the end: "Squashed 3 commits into one."
The combined commit gets the author of the oldest commit in the selection.
| Option | What it does | Default |
|---|---|---|
| Summary | The title of the combined commit. Required. | The first line of the combined messages (the oldest commit's) |
| Description | The body of the combined commit | The rest of the combined messages |
It's the interactive rebase engine: a git rebase -i from the
parent of the oldest commit in the selection, with the oldest one on pick and the other selected
ones combined into it. The message you wrote is written to the combined commit by an exec step with
git commit --amend.
git -c rebase.abbreviateCommands=false -c rebase.updateRefs=false \
-c rebase.missingCommitsCheck=ignore -c rebase.rebaseMerges=false \
rebase -i --autostash --no-autosquash --empty=drop --no-fork-point a1b2c3d^
# to-do list: pick a1b2c3d / fixup b2c3d4e / fixup c3d4e5f / (exec that writes the message) / pick ...
On a stack branch, the squash goes through commit surgery, so that the branches above come along. Then the dialog is the surgery one: "Squash 3 commits", with the plan, and the Squash button.
Ctrl+Z ("Undo: Squash 3 commits") brings the separate commits back. If the rebase stopped at a conflict, Abort puts everything back; if you took it to the end, it goes into Ctrl+Z as a whole when it finishes, with the same name.
When the selection doesn't work, the item shows the reason ("Squash 3 commits… — reason"):
- "the commits aren't adjacent": only consecutive commits can be combined. For the other cases, use the interactive rebase.
- "some commits aren't on feature/login": all of them have to be on the current branch's line.
- "there's a merge in the range": there's a merge between the oldest chosen commit and HEAD.
- "the selection includes a stash"
- "an operation is in progress"
Other warnings:
- "This rewrites history: the commits involved get new SHAs. If they're already on the remote, you'll need to force push.": with commits already pushed, the warning says how many. After the squash, send it with Force push… (Force push).
- "The selection is no longer on the current branch. Refresh the graph and select again."
Brings all the changes of another branch into the current branch as a single commit, with a prepared message (a summary and the list of the commits brought in). The other branch's history isn't linked to the current one's: the new commit has only one parent.
It's the local equivalent of merge requests' "squash on merge": useful for integrating an experimental branch, or one with many attempts, without bringing along all its commits.
- The menu of another branch (a label in the graph or a row in the left panel, local or remote) › Squash merge feature/login into main…, right below Merge feature/login into main.
- In a commit's menu, the submenu of each branch pointing to it has the same item.
- Check out the branch that will receive the commit, for example
main. - Right-click the
feature/loginbranch (in the graph or in the left panel) and choose Squash merge feature/login into main…. - The "Squash merge feature/login into main" dialog shows how many commits go in and the prepared
message:
- Summary: "Squash merge feature/login into main";
- Description: a
* <title>item for each commit brought in, from oldest to newest.
- Adjust the message and, if needed, check Skip hooks on the commit (--no-verify).
- Click Squash merge. If conflict prediction is on and finds a conflict, the "Before merging" window shows the files and offers Merge anyway.
- At the end: "Squash merged feature/login into main".
| Option | What it does | Default |
|---|---|---|
| Summary | The commit's title | "Squash merge feature/login into main" |
| Description | The commit's body | The list of the commits brought in, one per line, with * |
| Skip hooks on the commit (--no-verify) | The commit doesn't run the pre-commit and commit-msg hooks | Unchecked |
git merge --squash feature/login
git commit -q -F - # with the dialog's message; --no-verify if you checked the option
The message is also written to .git/SQUASH_MSG. That way, if the commit doesn't go through
(conflict or a hook that rejected it), a git commit from the terminal uses the same message.
The commits listed in the description are the ones on feature/login that aren't on main,
without merges (git log --reverse --no-merges main..feature/login).
- Conflict: "The squash merge hit conflicts. Resolve the conflicted files and commit: the
prepared message is already in the commit field." The files show up under "Conflicted" in the
changes panel. Resolve them in the conflict editor and
commit through the commit box.
git merge --squashdoesn't leave a merge in progress, so there's no operation bar with Abort: to give up, discard the changes (see Discard changes). - Commit rejected (a hook, signing…): "The commit wasn't made, but the squash result is staged and the prepared message went to the commit field." Fix the problem and commit through the commit box.
In both cases, the commit box gets the message on its own when it's empty. If you had already written something, the "Prepared message from the squash merge of feature/login" strip shows up, with Use and the ✕ "Dismiss the prepared message".
Ctrl+Z ("Undo: Squash merge of feature/login") removes the squash merge
commit, if it was created. The feature/login branch doesn't change at any point.
After a squash merge, git doesn't consider feature/login merged into main (its commits aren't
ancestors of main). To delete the branch, you'll have to force the deletion (see
Create, rename and delete branches).
- "Nothing to bring in: everything in feature/login is already in main.": there are no new commits.
- "You're on a detached HEAD: check out the branch that will receive the squash merge."
- "The commit message is empty.": the summary is required.
- The conflict prediction before the squash merge is the same as for a regular merge (needs git 2.38 or newer); see Predict conflicts before operating.
- The conflict prediction before the squash merge is the merge and rebase one: Preferences › Checks › "Conflict prediction" › "Before merging or rebasing" › Warn when it would conflict (see Checks).
- The commit made through the squash merge dialog goes through git hooks (unless you check Skip
hooks on the commit (--no-verify)), but not through Versora's commit checks (secrets, large
files, protected branch) or the
.versora.ymlrules. When the commit is made through the commit box (after a conflict or a hook that rejected it), those checks apply normally.
- Merge and rebase: the regular merge and fast-forward.
- Merge requests: the squash done by the server when the MR is merged.
- Branch hygiene: branch cleanup recognizes the ones that went in by squash through the merged MR.