Hunk Staging and Merge Conflicts
This page goes deeper than the Git Panel overview on two jobs you do from inside the editor, one hunk or one conflict at a time:
- Hunks. Move between the changed regions of a file, then stage, unstage or revert one of them without touching the rest of the file.
- Merge conflicts. Find the conflict blocks git left in a file and keep ours, theirs or both, one block at a time or from a list of all of them.
Every command here is on the Git menu and in the command palette (type
> and part of the title). None of them has a default key in the shared
keymap; the Zed keymap binds
Previous Change to Alt+, and Next Change to Alt+.. Vix runs your
own git for all of it, so it works in any repository your git can read.
For a guided walk-through on a throwaway repository, see Tutorial 4: The Git Workflow.
What a hunk is
A hunk is a run of adjacent changed lines in the active buffer compared with
the file’s last commit (HEAD). Vix compares the buffer, not the file on
disk, so unsaved edits count. A deleted block has no lines left in the
buffer; Vix treats it as sitting on the line just after the deletion, so put
the cursor on that line to act on it.
The hunk commands always compare with HEAD. Git → Change Gutter Base…
changes only what the change gutter colors (for
example index, so staged hunks stop being marked). It does not change which
hunks these commands find.
Hunks need a saved file inside a git repository: in an untitled buffer, or with no changes, every hunk command reports No changes against HEAD.
Moving between hunks
- Git → Next Change (
git.diff_next) moves the cursor to the first line of the next hunk below it. - Git → Previous Change (
git.diff_prev) moves to the previous one.
Both wrap: past the last hunk, Next Change goes back to the first, and Previous Change before the first goes to the last. The same two commands are also on the Go menu.
Stage Hunk
Git → Stage Hunk (git.stage_hunk) stages the hunk under the cursor and
nothing else:
- Vix starts from what is staged for the file now (or from
HEADwhen nothing is staged). - It replaces that hunk’s region with the hunk’s lines from the buffer.
- It writes the result as the file’s index entry (
git hash-object, thengit update-index --cacheinfo).
The buffer and the file on disk do not change. Because the staged lines come from the buffer, you can stage an edit you have not saved yet. The status line says Hunk staged.
Vix refuses, and changes nothing, when:
| Status line | Why |
|---|---|
| No change hunk at the cursor | The cursor is not inside a hunk. |
| Stage hunk failed: index diverged | The index no longer holds the committed text for this region. This usually means you staged the hunk and then edited it again; unstage the old version first. |
| Stage hunk failed: outside workspace | The file is not under the workspace root. |
Git → Diff with Staged opens the file’s diff against the index, so you can
check what you staged. Then commit from the Changes panel (c).
Unstage Hunk
Git → Unstage Hunk (git.unstage_hunk) is the reverse. It puts the
committed text back into the index in place of the hunk’s lines, and leaves
the other staged changes, the buffer and the file on disk alone. The status
line says Hunk unstaged.
The index must still hold the hunk exactly as the buffer shows it. If you edited the hunk after staging it, change it back first, or Vix refuses:
| Status line | Why |
|---|---|
| Unstage hunk failed: nothing staged | The file has no index entry to change. |
| Unstage hunk failed: hunk not staged | The index does not hold the buffer’s lines for this hunk at that position. |
Revert Hunk
Git → Revert Hunk (git.revert_hunk) replaces the hunk under the cursor
with its committed text, in the buffer only. Nothing is written to disk
or to the index until you save. The buffer is marked modified, the cursor
moves to the start of the hunk, and the status line says Hunk reverted to
HEAD. The revert is one undo step, so Undo brings the edit back.
Getting into a merge conflict
A merge stops on a conflict when both branches changed the same lines. In Vix that happens with:
- Git → Branch → Merge… (
git.merge_branch): choose a local branch and Vix runsgit merge <branch>into the current branch. - Git → Pull → Merge (
git.pull_merge), which runsgit pull --no-rebase.
git’s output, including the list of conflicting files, streams to the bottom
dock. Conflicted files then show the magenta U badge in the file explorer
and the U letter in the Changes panel.
What Vix reads as a conflict
A conflict block is the text between git’s markers:
<<<<<<< HEAD
our lines
=======
their lines
>>>>>>> other-branch
The lines above ======= are ours (the branch you were on) and the lines
below are theirs (the branch coming in). A marker is any line that starts
with seven <, = or > characters; the labels after them are ignored.
Limits worth knowing before you resolve:
- diff3 style is not understood. With
merge.conflictStyle = diff3(orzdiff3), git adds a|||||||line and the common ancestor’s lines. Vix treats them as part of ours, so Keep Ours would keep them too. Use git’s default style, or delete that section by hand. - Nested conflicts are not supported.
- An unfinished block ends the search. If a block is missing its
=======or>>>>>>>line, that block and every block after it are not found.
Resolving one conflict at a time
- Git → Next Conflict (
git.conflict_next) moves the cursor to the<<<<<<<line of the next conflict. It skips the block the cursor is in and wraps to the first block after the last one. - Git → Resolve → Keep Ours (
git.conflict_ours) replaces the whole block, markers included, with our lines. - Git → Resolve → Keep Theirs (
git.conflict_theirs) keeps their lines. - Git → Resolve → Keep Both (
git.conflict_both) keeps ours followed by theirs.
The Resolve commands act on the block the cursor is in, or on the next block below it when the cursor is between blocks. Each one edits the buffer as a single undo step, marks it modified, puts the cursor where the block began, and says Conflict resolved. With no block at or after the cursor, they say No merge conflict here.
For example, with the cursor in the first block of
a
<<<<<<< HEAD
ours1
=======
theirs1
>>>>>>> x
mid
Keep Both leaves a, ours1, theirs1, mid. Later blocks stay as
they were.
The Conflicts list
Git → List Conflicts… (git.conflict_list) opens the Conflicts
overlay for the active buffer. Each row is a number and the first line of
each side, ours on the left:
1: ours1 → theirs1
2: ours2 → theirs2
↑↓ select Enter go to o/t/b resolve Esc close
| Key | Action |
|---|---|
↑ / ↓, PageUp / PageDown, Home / End | Move the highlight |
Enter or a click on a row | Jump to that conflict and close the list |
o / t / b | Keep ours / theirs / both for the highlighted conflict, and stay in the list |
Esc | Close the list |
After each o, t or b the list is rebuilt from the buffer, so it shrinks
by one. It closes by itself when the last conflict is resolved. If the buffer
has no conflicts, the list does not open and the status line says No merge
conflict here.
The list covers the active buffer only. To find the other conflicted files,
look for the U rows in Git → Changes….
Finishing the merge
Resolving a block only edits the buffer. To finish:
- Save the file.
- In Git → Changes…, select the file and press
s(orSpace) to stage it. Staging is how git learns the conflict is resolved. - When every conflicted file is staged, press
cand commit. git records the merge commit.
A conflict during an interactive rebase is finished the same way, except that
step 3 is Git → Rebase → Continue Rebase (or Abort Rebase to give
up). Vix has no command to abort a merge; run git merge --abort in the
terminal.
See also
- Git Panel: whole-file staging, commits, branches.
- Change Gutter Base: what the gutter compares with.
- Branch Review: stage or discard hunks while reviewing a whole branch.
- Interactive Rebase.
- Specs:
crates/vix-git/spec/stage-hunk/index.md,crates/vix-git/spec/unstage-hunk/index.md,crates/vix-conflict-tool/spec/index.md.
Vix™ and Vix IDE™ are trademarks.