Skip to content
Versora

Manual

History

Commit surgery

20 min read

On this page

Commit surgery brings together the fixes that come up most often for anyone who keeps a branch clean: sending a fix to the right commit, combining two commits, taking a commit out of the middle without losing the work and splitting a large commit. Instead of you building an interactive rebase, Versora finds what needs to be redone, shows the plan and does everything in a single rewrite.

How commit surgery works

All the features on this page follow the same rules.

The editable range. Surgery only touches the commits of the current branch, above the nearest main branch (main, master, develop or trunk, local or remote, or the remote's default branch). On a stack branch, the range starts above the parent of the stack's first branch. On the main branch itself, that leaves editable only the commits that haven't gone to its remote yet. A commit outside the range shows up with the reason, for example "Squash into the previous commit… — already in main".

The plan beforehand. Before changing anything, surgery shows "What will happen":

  • how many commits are rebuilt and from where ("Rebuilds 3 commits on top of 9f8e7d6 Set up routes.");
  • which stack branches come along ("The stack comes along: feature/api (below), feature/ui (above).");
  • who will need a force push ("Already pushed: feature/login will need a force push.");
  • whether there are changes outside the operation ("Changes that aren't part of the operation go into an automatic stash and come back at the end.");
  • whether and where the operation is expected to stop at a conflict ("Expected to stop on a conflict at b2c3d4e Validate email on login (src/login.ts): resolve it in the conflict editor and continue from the changes panel.");
  • "Ctrl+Z undoes the whole operation (every branch)."

The conflict prediction simulates each step in memory and is skipped for long ranges (more than 150 commits): "Long range: conflicts weren't predicted."

A single rewrite. Each operation is a single git rebase -i with the to-do list built by Versora. The commits above are rebuilt on top of the new version, and the stack branches follow: the ones below whose tip is in the range and the ones above, which go on top of the new version. Git only moves the references at the end.

Conflict. If a step doesn't apply, the rebase stops in the usual conflict editor: "Stopped on a conflict at b2c3d4e Validate email on login. Resolve the files under "Conflicted" in the changes panel and click Continue. Abort leaves everything as it was, staged changes included." See Operation in progress.

Undo. Ctrl+Z takes all the branches back at once. In the operations that take the staged changes into a commit (fixup, amend and absorb), the staging area also goes back exactly as it was. If the operation stopped at a conflict and you clicked Continue until the end, it goes into Ctrl+Z when it finishes, as a whole. Abort leaves everything as it was: "Operation aborted: everything went back to how it was, staged changes included."

Requirements. Git 2.40 or newer, a branch checked out (not detached HEAD) and no stopped operation in the repository. Each operation recreates at most 1000 commits.

Where the features are

FeatureCommit box barCommit menuCurrent branch menuPalette (Ctrl+P)
Targeted fixupCommit as fixup of…Create fixup for this commit…—"Commit as fixup of…", "Find the source commit of the staged changes"
Apply pending fixupsApply 2 fixups (only when there are pending ones)—Apply pending fixups / Apply 2 pending fixups"Apply pending fixups"
AbsorbAbsorb…——"Absorb: each staged hunk into its commit…"
Amend a commit—Amend this commit with the staged changes…——
Squash into the previous one—Squash into the previous commit…——
Uncommit—Uncommit (bring the changes back to the stage)…——
Split—Split commit…——

The commit menu items are right below Edit commit message…. When one doesn't apply, it shows the reason: "a rebase is in progress", "detached HEAD: check out a branch", "nothing staged", "there are conflicted files", "merge commit", "the repository's first commit", "already in main", "no previous commit on the branch", "the previous commit isn't on the branch" or "no pending fixup!".

Commit as fixup of…

Sends what is staged to a commit on the branch, instead of creating a new commit on top. It's the "I forgot a line in the commit from three commits ago": you fix the file, stage it and choose the target commit. Versora already suggests which one it is, from the blame of the staged lines.

You choose when to apply it: now (the chosen commit gets the staged changes and the ones above are rebuilt right away) or later (a fixup! <subject> commit stays on top, to be applied with "Apply pending fixups" whenever you want, for example before the merge request).

Where to find it

  • The "Commit surgery" bar in the commit box › Commit as fixup of…
  • The commit's menu › Create fixup for this commit… (opens the same dialog with that commit already chosen).
  • Palette: "Commit as fixup of…" or "Find the source commit of the staged changes".

Step by step

  1. Fix the file, for example src/login.ts, and stage the fix (see Staging).
  2. In the commit box, click Commit as fixup of….
  3. Check the source box. It says where the staged lines come from:
    • "The staged lines come from a1b2c3d Add login screen.": the commit is already chosen;
    • "The staged lines come from 2 commits of the branch:", with the list: choose one target for everything, or use Absorb… (section below) to send each hunk to its own commit;
    • "The source can't be found: only new or binary files are staged. Choose the commit."
  4. In Target commit, choose the commit (click, or arrows and Enter). A double-click chooses and confirms.
  5. In When to apply, choose Apply now or Leave the fixup! for later. Versora remembers the last choice.
  6. Check the plan and click Commit and apply (now) or Commit fixup! (later). Ctrl+Enter confirms and Esc closes.

Before the commit, the usual checks run (secrets, large files, protected branch…, see Checks).

OptionWhat it doesDefault
Target commitThe commit that gets the staged changes. The list shows the commits of the editable range, from newest to oldest (up to 300).The source commit, when there's only one
Apply nowThe chosen commit gets the staged changes, and the ones above (and the stack) are rebuilt right away.Selected the first time; afterwards, the last choice
Leave the fixup! for laterCreates the commit "fixup! Add login screen" on top of the branch, without rewriting anything now.—
Skip Git hooksComes from the commit box's "Commit options". With it checked, the dialog warns that "Skip hooks" is checked in the commit optionsUnchecked

What Versora does in git

Now: the staged changes become a regular commit (with the usual hooks and signing) and a rebase takes that commit to right after the target:

bash
git commit --cleanup=verbatim -F -     # message "fixup! Add login screen"
git -c rebase.abbreviateCommands=false -c rebase.updateRefs=false \
    -c rebase.missingCommitsCheck=ignore -c rebase.rebaseMerges=false -c rebase.autoSquash=false \
    rebase -i --autostash --no-autosquash --empty=drop --no-fork-point \
    --onto 9f8e7d6 9f8e7d6 feature/login
# to-do list: pick a1b2c3d / fixup <staged commit> / pick b2c3d4e / ...
# plus "update-ref refs/heads/<branch>" for each stack branch that comes along

9f8e7d6 is the target commit's parent: the range is rebuilt from it. Unstaged changes go into the automatic stash and come back at the end.

Later: just the commit, as in the commit box:

bash
git commit -m "fixup! Add login screen"

The "Commit options" apply to it (skip hooks and, if checked, push after committing).

How to undo

  • Apply now: Ctrl+Z ("Undo: Fixup into a1b2c3d") takes all the branches back and returns the staging area exactly as it was.
  • Leave the fixup! for later: Ctrl+Z ("Undo: fixup! commit") undoes the commit and puts the changes back in staging.

Caveats and messages

  • "The staged lines come from before the branch, from commits already in main (…). A fixup doesn't fit here: make a new commit.": the lines you changed don't belong to any commit on the branch.
  • "Some of the lines come from before the branch (from main).": part of the staged changes comes from commits that aren't on the branch; check the target.
  • "No source commit (new or binary files, or lines between different commits): …": those files go to the target you choose.
  • "No commits on the branch after main.": the branch has no commits of its own.
  • "Only the most recent commits are listed.": the branch has more than 300 commits in the range.
  • "Nothing is staged. Stage the changes that go into the commit.": stage first.
  • "Created a fixup! commit for "Add login screen". Apply it whenever you want.": the notice comes with the Apply pending fixups button.
  • "The staged changes went into "Add login screen".": the fixup was applied. If there were pushed commits, the notice adds "Commits that were already pushed were rewritten: the next push needs to be forced."

"Skip Git hooks" and "Push after committing" are in the "Commit options" (see The commit box). No policy key changes fixup.

See also

Apply pending fixups

Folds all the branch's fixup!, squash! and amend! commits into their target commits at once. It's git's autosquash without having to open a rebase: it works both for the fixups created with "Leave the fixup! for later" and for the ones you created in the terminal (git commit --fixup).

Where to find it

  • The current branch's menu › Apply pending fixups (with the count once it has been read: Apply 2 pending fixups).
  • The "Commit surgery" bar › Apply 2 fixups (only shows up when there are pending fixups).
  • In the notice that shows up after creating a fixup!: the Apply pending fixups button.
  • Palette: "Apply pending fixups" ("Folds the branch's fixup!, squash! and amend! commits into their target commits").

Step by step

  1. Choose Apply pending fixups.
  2. The "Apply pending fixups?" confirmation lists what goes where, for example: "2 fix commits go into their target commits on feature/login:" and the lines "fixup! e7f8a9b → a1b2c3d Add login screen". The ones that don't find a target show up separately.
  3. Check the plan and click Apply. At the end: "2 fixups applied."

There are no options. Each commit's type decides what happens to it:

Commit subjectWhat happensIn the rebase list
fixup! <subject>The changes go into the target; the target's message stays.fixup
squash! <subject>The changes go into the target, and the messages are combined, as in git's autosquash.squash
amend! <subject>The changes go into the target, and the amend! message replaces the target's.fixup -C

The target is found the way git does it: the oldest commit on the branch with the same subject; otherwise, the commit whose SHA starts with the text after the prefix; otherwise, the commit whose subject starts with that text. Repeated prefixes (fixup! fixup! …) also work.

What Versora does in git

bash
git -c rebase.autoSquash=false ... rebase -i --autostash --no-autosquash --empty=drop \
    --no-fork-point --onto <parent of the oldest target> <parent of the oldest target> feature/login
# each fixup!/squash!/amend! leaves its position and goes right after its target

The same command as in the previous section, with the to-do list built by Versora instead of git's --autosquash.

How to undo

Ctrl+Z ("Undo: Apply 2 fixups") takes the branches back, with the fixup! commits back in place.

Caveats and messages

  • "There are no pending fixup!, squash! or amend! commits on this branch.": nothing to apply.
  • "The fixup! commits on this branch don't match any of its commits (the target is already in the main branch or its subject changed).": the target commit isn't in the editable range, or its subject changed after the fixup.
  • "1 stays where it is (no commit on the branch has that subject): "fixup! Adjust CSS".": that commit isn't applied; the others are.

See also

Absorb…

Distributes the staged changes among the branch's commits: each hunk goes to the commit that last touched those lines. It's for when you've made several small fixes in different files, each one belonging to a different commit on the branch. A preview shows each hunk's target, and you can change it or leave the hunk staged.

Where to find it

  • The "Commit surgery" bar › Absorb… ("Sends each staged hunk to the commit that last touched those lines").
  • Palette: "Absorb: each staged hunk into its commit…".

Step by step

  1. Stage the fixes.
  2. Click Absorb…. While it reads, the dialog shows "Reading the blame of the hunks…".
  3. For each hunk, check the target in the selector. The suggested commit has "(suggested)". Choose another commit, or Stays staged to leave that hunk alone.
  4. Check the summary ("2 hunks will be absorbed (targets: 2)." and "1 stays staged.") and the plan.
  5. Click Absorb 2 hunks (or Ctrl+Enter). At the end: "Absorb finished: 2 hunks went to their source commits (targets: 2)."
OptionWhat it doesDefault
Each hunk's targetA commit from the editable range, or Stays stagedThe commit that last touched the lines, when there's only one and it's on the branch

Some hunks have no suggested target and stay staged, unless you choose one:

  • "The lines come from before the branch (already in main): it stays staged unless you choose a target."
  • "New lines between different commits (a1b2c3d Add login screen and b2c3d4e Validate email on login): choose the target."

Files that don't change line by line always stay staged, with the selector disabled: "New file: there's no source commit (it stays staged).", "Deleted file: …", "Binary file: …", "Submodule: …", "Symbolic link: …", "File permission change: it stays staged."

Unstaged changes don't take part: "Unstaged changes stay where they are."

What Versora does in git

Versora reads the diff of the staged changes without context (-U0) and runs blame on each hunk's lines to find the commit on the branch that last changed them. Then it builds a fixup! commit for each target, directly with git commit-tree (without going through git's commit hooks; Versora's checks, such as the secrets one, run first), and does a single rebase with fixup right after each target, as in the "Commit as fixup of…" section. The hunks left without a target go back to the stage at the end.

How to undo

Ctrl+Z ("Undo: Absorb 2 hunks") takes the branches back and returns the staging area exactly as it was.

Caveats and messages

  • "Choose a target for at least one hunk.": all of them are on Stays staged.
  • "No hunk has a target commit: nothing to do.": nothing to absorb.
  • "The staged changes changed since the preview. Open the preview again.": you touched the staging area with the dialog open.
  • "The hunks that didn't go into any commit are back in the working directory, but unstaged.": Versora couldn't put them back in staging; stage them again.

See also

  • The "Commit as fixup of…" section, on this page, to send all the staged changes to a single commit.
  • Blame.

Amend this commit with the staged changes…

Amend for a commit in the middle of the branch: what is staged goes into the chosen commit, and you can change its message in the same step. The commits above are rebuilt on top.

Where to find it

  • The commit's menu › Amend this commit with the staged changes… (needs something staged).

Step by step

  1. Stage the changes.
  2. Right-click the commit, for example a1b2c3d Add login screen, and choose Amend this commit with the staged changes….
  3. The "Amend commit a1b2c3d" dialog explains: "2 staged files go into this commit and the commits above are rebuilt on top of it. You can change the message too." and shows the plan.
  4. Adjust the Summary and the Description, if you want, and check Skip Git hooks (pre-commit, commit-msg) if that's the case.
  5. Click Amend. At the end: "Amended "Add login screen"."
OptionWhat it doesDefault
Summary and DescriptionThe commit's message after the amendThe current message
Skip Git hooks (pre-commit, commit-msg)The commit of the staged changes is made with --no-verifyUnchecked

What Versora does in git

It's the "now" fixup from the "Commit as fixup of…" section. If you changed the message, the commit of the staged changes carries the new message and goes into the list as fixup -C, which replaces the target's message; if you didn't, it goes in as a regular fixup.

How to undo

Ctrl+Z ("Undo: Amend a1b2c3d") takes the branches back and returns the staged changes.

Caveats and messages

  • "nothing staged" (on the menu item) or "Nothing is staged. Stage the changes that go into the commit.": stage first.
  • "there are conflicted files": resolve the conflicts first.
  • "The commit message can't be empty.": the summary is required.
  • The commit checks (secrets, large files…) run first, as in a regular commit.

See also

Squash into the previous commit…

Combines the chosen commit with the commit right below it (the previous one) into a single commit, with the message you write. The commits above are rebuilt on top.

Where to find it

  • The commit's menu › Squash into the previous commit…
  • To combine more than two neighboring commits, select them in the graph and use Squash N commits… (see Squash). On a stack branch, that item also goes through here, so the branches above come along.

Step by step

  1. Right-click d4e5f6a Fix typo on the login screen and choose Squash into the previous commit….
  2. The "Squash 2 commits" dialog explains: "The 2 commits become one, with the message below; the ones above are rebuilt on top." The Summary and the Description start with both messages, in order.
  3. Adjust the message, check the plan and click Squash. At the end: "2 commits squashed into one."
OptionWhat it doesDefault
Summary and DescriptionThe message of the combined commitThe messages of both commits, separated by a blank line

What Versora does in git

Versora creates the combined commit with git commit-tree: the tree of the newer commit, the parent of the older one and the authorship of the older one. Then it does a single rebase in which that commit takes the place of both (pick), and the ones above are reapplied.

How to undo

Ctrl+Z ("Undo: Squash 2 commits") brings both commits back.

Caveats and messages

  • "no previous commit on the branch": the commit is the first one in the editable range.
  • "the previous commit isn't on the branch": the previous one is a merge or is already in the main branch.
  • "Only adjacent commits of the current branch can be squashed.": squashing only works for consecutive commits.
  • "The chosen commits cross the feature/api branch of the stack: squashing them would leave feature/api without its own commits.": the tip of another stack branch is between the commits.

See also

Uncommit (bring the changes back to the stage)…

Takes a commit out of the middle of the branch and returns its changes to the stage (and to the working directory). The commits above are rebuilt without it. It's for "taking apart" a commit and redoing it another way, without losing anything it had.

Where to find it

  • The commit's menu › Uncommit (bring the changes back to the stage)…

Step by step

  1. Leave the working directory clean (commit or stash the pending changes).
  2. Right-click the commit, for example c3d4e5f Add debug logging, and choose Uncommit (bring the changes back to the stage)….
  3. The "Uncommit" confirmation explains: ""Add debug logging" (c3d4e5f) leaves the branch and its changes go to the stage. The commits above are rebuilt without it. If they depend on those changes, nothing is done." and shows the plan.
  4. Click Uncommit. At the end: "The changes from "Add debug logging" are staged.", and the changes panel is selected.

What Versora does in git

A rebase that rebuilds the range without the commit. At the end, its changes are applied on top of the result with an in-memory tree merge and written to the stage and the working directory (git read-tree -m -u). Before starting, Versora checks in memory whether the commits above apply without the commit.

How to undo

Ctrl+Z ("Undo: Uncommit c3d4e5f") puts the commit back in its place.

Caveats and messages

  • "There are uncommitted changes: commit or stash them before uncommitting (the commit's changes go to the working directory).": clean the directory first.
  • "Without this commit, b2c3d4e Validate email on login doesn't apply (it touches the same lines in src/login.ts). The commits above depend on it: nothing was changed.": a commit above depends on this one. Use Move a file, hunk or lines between commits or the interactive rebase.
  • "This commit's changes don't apply cleanly on the result (src/login.ts): nothing was changed.": the changes don't fit on top of the rebuilt commits.
  • "The uncommit didn't finish and everything went back to how it was: …": something failed at the end; the repository went back to its previous state.

See also

  • Drop commit: removing the commit and throwing its changes away.
  • Reset: "Soft" does the same with the branch's last commits.

Split commit…

Splits a commit into several, choosing whole files, hunks or lines for each part. Each part becomes a commit, in order, with its own message and the original commit's authorship. The commits above and the branches above in the stack follow.

Where to find it

  • The commit's menu › Split commit…

Step by step

  1. Right-click the commit, for example a1b2c3d Add login screen, and choose Split commit….
  2. The "Split commit a1b2c3d" dialog opens with two parts. Part 1 starts with the original commit's message; the last one, "Part 2 (the rest)", gets everything you don't choose.
  3. With Part 1 active, choose what goes into it:
    • click the changed lines in the diff (Shift+click takes a range); clicking a line of the active part again returns the line to the rest;
    • Hunk → Part 1 takes the whole hunk;
    • Whole file → Part 1 takes the file; Back to the rest undoes it.
  4. For more parts, click Part (up to eight). With three parts or more, the ✕ on each tab removes the part ("Remove part 2"); what was in it goes back to the rest.
  5. Write each part's message in "Message of Part N".
  6. Check the plan and click Split into 2 commits (or Ctrl+Enter). At the end: "Commit split into 2 commits."
OptionWhat it doesDefault
PartsFrom 2 to 8 parts; the last one always gets the rest2
Each part's messageThe commit message for that partPart 1: the original's; the others, empty
Lines, hunks and filesEach part's contentEverything in the last part

Files that don't change line by line (new, deleted, binary, submodule, symbolic link, permission change) go whole into one part, for example "New file: it goes into one part as a whole." Files with more than 4000 lines in the diff don't show all the lines: "File too long to show every line: use "Whole file" or the hunk buttons."

What Versora does in git

For each part, Versora builds the tree with the chosen lines (without touching the working directory) and creates the commit with git commit-tree, keeping the original's author and date. Then a single rebase puts the new commits in place of the original and reapplies the ones above, with the stack along with it.

How to undo

Ctrl+Z ("Undo: Split a1b2c3d into 2 commits") brings back the original commit.

Caveats and messages

  • "The commit has no changes to split.": the commit is empty.
  • "This commit's diff is too large for the split screen.": use the interactive rebase with "Edit (stop to amend)".
  • "Split into at least two parts."
  • "The message of part 2 is empty.": every part needs a message.
  • "Part 1 has no changes: choose what goes into it, or remove the part."
  • "The selection in src/login.ts no longer matches the commit. Open the split again.": the commit changed with the dialog open.

See also

Messages common to all of commit surgery

  • "Commit surgery needs git 2.40 or newer (this one is 2.39). Update Git for Windows."
  • "You're on a detached HEAD: check out a branch before editing its commits."
  • "Commit a1b2c3d is already in main, outside the feature/login branch: editing it would rewrite the main branch history."
  • "Commit a1b2c3d isn't on the feature/login branch line."
  • "The repository's first commit can't be edited here. Use the interactive rebase."
  • "a1b2c3d is a merge commit: this operation only works on regular commits."
  • "There are merge commits in the part that would be rebuilt: the operation only replays linear history. Use the interactive rebase or rebase the branch first."
  • "feature/ui is in use in the worktree D:\projetos\loja-ui: close that worktree or run the operation from there.": a stack branch is checked out in another worktree.
  • "The operation would recreate 1200 commits (the maximum is 1000)."
  • "A rebase is in progress: finish or abort it first."
  • "The branch changed after it was checked. Review the plan again.": the branch moved between the plan and the confirmation.
  • "The unstaged changes couldn't be reapplied without conflicts: they're kept in the stash list.": apply the stash (see Stash).
  • "Operation aborted. The changes came back, but some of them are now unstaged.": after Abort, stage again whatever is missing.
  • "git returned the diff in an unexpected format (external diff configured?).": a git diff setting changed the format Versora reads.