History
Move a file, hunk or lines between commits
9 min read
Sometimes a change ended up in the wrong commit: the new function went into the "Adjust styles" commit, or a fix for something else got bundled with a big change. This feature takes exactly what you choose (a whole file, a hunk or just a few lines) out of the source commit and puts it in another commit on the branch, or in a new commit right above or right below a chosen commit. The rest of the source commit stays where it is.
It uses the same engine as commit surgery: the editable range is the same (the current branch's commits above the main branch), everything runs in a single rewrite, with a plan beforehand, the stack follows and Ctrl+Z takes all the branches back.
By dragging (the destinations are the current branch's commits in the graph):
- A file: in a commit's file list (right panel, with the commit selected in the graph), drag the file's row onto another commit on the branch.
- A hunk: in a commit's diff, drag the Move… button in the hunk header. When the file can only go as a whole, the button becomes Move file… and the tooltip says why.
- A few lines: in a commit's diff, choose the lines by their numbers (Shift+click takes a range) and drag the Move lines to commit… button on the lines bar.
From the keyboard or the menu (opens the "Move changes between commits" dialog):
- A file's menu in the commit's list › Move to commit…
- The commit's menu › Move changes to another commit… (right below the surgery items).
- A click (without dragging) on Move…, Move file… or Move lines to commit….
- Palette (Ctrl+P): "Move changes from the selected commit to another commit…", with a commit of the current branch selected in the graph.
While you drag, the branch's commits that accept the changes are outlined, and the label next to the cursor says what will happen:
| Where you drop | What happens | Text next to the cursor |
|---|---|---|
| In the middle of a commit's row | The changes go into that commit. | "Move the hunk in login.ts into b2c3d4e Validate email on login" |
| On the row's top edge | They become a new commit right above it (newer). | "the hunk in login.ts into a new commit right above b2c3d4e …" |
| On the row's bottom edge | They become a new commit right below it (older). | "the hunk in login.ts into a new commit right below b2c3d4e …" |
| On a place that doesn't accept it | Nothing; the label says why. | "the hunk in login.ts — merge commit" |
The edges are the top and bottom strips of each row (a little less than a third of the height each). Near the edges of the list, it scrolls on its own. Esc cancels the drag.
When the graph isn't on screen (a diff or blame takes up the central area), the "Drop on a commit of feature/login" list shows up on top ("On a commit: goes into it. Between two: becomes a new commit."), with the branch's commits and the gaps between them: "+ New commit here", "+ New commit at the start of feature/login" and "+ New commit at the end of feature/login".
Dropping doesn't change anything yet: it opens the dialog with the selection and the destination already chosen, and only the confirmation runs it.
- Header: "Source: c3d4e5f Adjust button styles (feature/login). Choose what leaves and where it goes; the rest of the commit stays where it is."
- What to move: the commit's files. Check a whole file, or expand it (the arrow next to it, or →/← on the keyboard) and check hunks. Lines chosen in the diff show up as "3 lines selected in the diff". You can also drag an item from here onto a destination on the right; if the item was already checked, the whole selection goes.
- Where to: the commits of the editable range ("commits above main") and the gaps between them ("+ New commit here", "+ New commit at the start of feature/login", "+ New commit at the end of feature/login"). The source commit has the "source" badge. A destination that doesn't accept it is grayed out, with the reason in the tooltip.
- New commit message: shows up when the destination is a gap. The Summary is required; the "Description (optional)" isn't.
- What will happen: the plan, rebuilt with each choice.
- Buttons: Cancel and Move (or Create commit and move, when the destination is a new commit). Ctrl+Enter confirms and Esc closes.
Example: the commit c3d4e5f Adjust button styles took along a validation function that belongs to
the commit b2c3d4e Validate email on login.
- Select
c3d4e5fin the graph and open the diff ofsrc/login.tsin the right panel. - On the validation function's hunk, drag the Move… button onto the middle of the
b2c3d4erow in the graph. (Or click Move… and chooseb2c3d4eunder Where to.) - In the dialog, check the plan:
- "Leaves c3d4e5f Adjust button styles (feature/login): src/login.ts (1 hunk)."
- "Goes into b2c3d4e Validate email on login (feature/login)."
- If there are commits between the two: "The commit between the two will now have these changes." (the destination is older) or "The commit between the two will no longer have these changes." (the destination is newer).
- Click Move. At the end: "Changes moved into "Validate email on login": the hunk in login.ts."
For a new commit, drop on a commit's edge (or choose a gap), write the summary and click Create commit and move. The new commit gets the source commit's authorship, and the final notice is, for example: "New commit "Extract email validation" created with 3 lines from login.ts."
| Option | What it does | Default |
|---|---|---|
| What to move | Whole files, hunks or lines chosen in the diff | What you dragged or chose before opening it |
| Where to | A commit from the editable range, or a new commit between two | The place where you dropped |
| New commit message | Summary and description of the new commit | Empty |
Some files can only go as a whole, and the dialog shows the reason ("new file: whole file only"): a new file, a deleted file, binary, submodule, symbolic link, mode change and a renamed file (the rename and its changes go together). In a very large diff, the list shows "large diff: pick hunks in the commit diff": check the whole file there, or choose the hunks in the commit's diff with each one's Move… button.
What leaves (Δ) is part of the source commit's diff against its parent. Without touching the working
directory, Versora builds the necessary trees, creates the new commits with git commit-tree and
does a single rebase, with the same configuration as surgery:
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 <parent of the oldest commit> <parent of the oldest commit> feature/login
- Destination older than the source: a
fixupright after the destination takes Δ to it, and the source then applies only what's left of it. - Destination newer than the source: the source loses Δ, and the destination then applies what it already had plus Δ in one go. If a commit in the middle depends on Δ, the rebase stops at the conflict where it should.
- New commit: goes right above or right below the chosen commit, with the message you gave and the source's authorship.
- If Δ was everything the source commit had, it becomes empty and leaves the history.
- The commits in the middle and above are reapplied as they are, and the stack branches follow
(
update-ref).
Ctrl+Z ("Undo: Move between commits (c3d4e5f → b2c3d4e)", or "(c3d4e5f → new commit)") takes all the branches back as they were. If the move stopped at a conflict, Abort leaves everything as it was; if you continued until the end, the operation goes into Ctrl+Z when it finishes.
In the plan:
- "c3d4e5f Adjust button styles is left with no changes and leaves the history.": you're moving everything the commit had.
- "feature/ui is left with no commits of its own (on the same commit as the branch below).": in a stack, the branch above would be empty.
- "Already pushed: feature/login will need a force push.": see Force push.
- "Expected to stop on a conflict at …": the prediction found a conflict. The reason comes right
below:
- "What's moving down depends on something that only arrives in a commit between the two: in the destination, those lines don't exist yet (or are different)."
- "In the destination, the same lines (or the ones next to them) were already changed by the commits between the two."
- "A commit between the two touches the same lines (or the ones next to them) as what's moving down."
- "A commit between the two depends on what's moving up (it touches the same lines or the ones next to them): without it, that commit doesn't apply."
- "The rest of the source commit doesn't apply without a conflict."
- "Resolve it in the conflict editor and continue, or cancel and choose another destination."
- "Many commits: conflicts were not predicted."
While you build it: "Check what will change commits.", "Choose the destination: a commit, or "+ New commit" between two." and "Enter a summary for the new commit."
Why a commit can't be a source (the menu item or the button shows up disabled with the reason):
- "detached HEAD: check out a branch"
- "a rebase is in progress"
- "merge commit: its changes can't be moved from here"
- "the repository's first commit: use interactive rebase"
- "only commits of feature/login above main can be edited"
- "Turn off "Ignore whitespace" to move a hunk: the whitespace-insensitive diff doesn't match the commit": turn the option off on the diff bar (see Reading the diff).
Why a destination doesn't work, while dragging: "outside the current branch (already in main)", "merge commit", "already in this commit (drop on the top or bottom edge for a new commit)" and "not a commit".
From the engine:
- "The changes are already in that commit: choose another destination."
- "src/logo.png can only be moved as a whole (binary file)." and "src/old.ts was renamed in this commit: it can only be moved as a whole (the rename and its changes go together)."
- "The selection in src/login.ts no longer matches the commit. Open the diff again.": the commit changed after you chose the lines.
- "Commit c3d4e5f has no changes to move."
- The messages common to surgery (git 2.40, detached HEAD, merges in the range, worktree in use, outdated plan) are in Commit surgery.
No preferences of its own. The feature requires git 2.40 or newer.
- Commit surgery: "Split commit…" splits a commit into several new commits.
- Worktrees: "Move to another worktree" and "Cherry-pick into another worktree" take commits or uncommitted changes to another worktree.
- Dragging branches and commits: dragging a whole commit.
- Staging: choosing lines in the diff.