Sub topic 13
Less common modes: --keep and --merge
This chapter covers the two remaining git reset modes, --keep and --merge. It assumes you know --soft, --mixed, and --hard from the earlier chapters. You should also be comfortable reading git status --short and know that an index entry is a file's staged version.
You will rarely type these modes by hand. They matter because they sit between --mixed and --hard. Like --hard, they update the working directory. Unlike --hard, they refuse to destroy certain local edits.
Where these modes fit
--hard is blunt. It overwrites the index and every tracked file in the working directory, so uncommitted edits to tracked files are lost. --mixed is gentle but never touches the working directory, so the files on disk do not match the commit you reset to.
Sometimes you want both: move the branch, make the working directory match the new commit, and keep your unrelated uncommitted edits. --keep and --merge do this. Both compare three versions of each file: the current HEAD, the target commit, and your local copy. Then they decide file by file whether to update it or stop.
git reset --keep
git reset --keep <commit> resets the index and updates the files that differ between HEAD and <commit>. It keeps your local changes in files that do not differ between those two commits. If a file that differs between HEAD and <commit> also has local changes, Git aborts before changing anything.
The difference from --hard shows in this example. You remove the last commit and also have an uncommitted edit to an unrelated file.
git init keep-demo && cd keep-demo
echo one > file.txt
echo notes > notes.txt
git add .
git commit -m "first"
echo two >> file.txt
git commit -am "second"
echo "my edit" >> notes.txt
git reset --keep HEAD~1
git status --short
cat file.txtAfter the reset, git status --short prints M notes.txt. The edit to notes.txt survived, and file.txt contains only one. With --hard, the notes.txt edit would be gone.
Now the abort case. The commit you remove touched file.txt, and you have also edited file.txt locally.
git init keep-abort && cd keep-abort
echo one > file.txt
git add file.txt
git commit -m "first"
echo two >> file.txt
git commit -am "second"
echo "local edit" >> file.txt
git reset --keep HEAD~1Git refuses and prints an error about file.txt (the wording varies between versions). The branch, index, and working directory stay as they were. Commit or stash the edit, then retry.
Staged changes with --keep
--keep ignores the difference between staged and unstaged edits when the file is unrelated to the move. If you staged an edit to an untouched file, --keep resets the index entry to the target commit's version. The edit stays in the working directory as an unstaged change. If a file that differs between HEAD and the target has staged changes, Git aborts.
git reset --merge
git reset --merge <commit> was designed to back out of a failed merge. It resets the index and updates the files that differ between HEAD and <commit>. It keeps files with unstaged changes. If a file that differs between the index and <commit> has unstaged changes, Git aborts.
In a conflicted merge, the conflicted paths are unmerged in the index. A plain --mixed reset would leave the merged contents in the working directory. --merge restores the conflicted files to the pre-merge state and leaves your unrelated edits in place.
git init -b main merge-demo && cd merge-demo
echo base > a.txt
echo notes > notes.txt
git add .
git commit -m "base"
git switch -c feature
echo feature > a.txt
git commit -am "feature"
git switch main
echo main > a.txt
git commit -am "main"
echo "draft" >> notes.txt
git merge feature
git reset --merge
git status --shortgit merge feature stops with a conflict in a.txt. git reset --merge with no commit defaults to HEAD, so it returns the index and a.txt to the state of main. The final status shows only M notes.txt, which is your draft edit.
Relationship to git merge --abort
When a merge is in progress, git merge --abort is equivalent to git reset --merge. Use git merge --abort in practice, because its name says what you intend. The Git documentation warns that if you had uncommitted changes when the merge began, --abort may not be able to reconstruct them. This is why you should commit or stash before merging.
Comparing the modes on tracked files
The table shows what each mode does to one tracked file. "Differs" means the file's content differs between HEAD and the target commit.
| Situation for the file | --mixed | --hard | --keep | --merge |
|---|---|---|---|---|
| Unstaged edit, file does not differ | Edit kept | Edit discarded | Edit kept | Edit kept |
| Staged edit, file does not differ | Unstaged, kept | Discarded | Unstaged, kept | Discarded |
| Unstaged edit, file differs | Edit kept, index reset | Discarded | Aborts | Aborts |
| Staged edit, file differs | Unstaged, kept | Discarded | Aborts | Overwritten |
| No local edit, file differs | Index reset only | Updated | Updated | Updated |
Two rows are easy to misread. In the second row, --merge discards a staged edit even though it keeps unstaged ones, which is why it is not a safe general-purpose mode. In the fourth row, --keep is the safer mode, because it aborts instead of overwriting.
The --mixed column shows why neither new mode replaces it. --mixed never changes the working directory, so it never aborts and never loses an edit. The other modes make the working directory match the target.
Choosing a mode
Use this order when deciding.
- To keep the changes of the removed commits, use
--softor--mixed. - To make the working directory match the target and throw away everything, use
--hard. - To make the working directory match the target and keep unrelated uncommitted edits, use
--keep. - To abandon a conflicted merge, use
git merge --abort. Usegit reset --mergeonly in older workflows or scripts.
A common use of --keep is moving a branch back several commits while a half-finished edit sits in a file those commits never touched. --hard would lose the edit and --mixed would leave the old commits' contents on disk.
Failure modes and gotchas
- Aborts are all-or-nothing. If one file blocks the reset, Git changes nothing. Fix the conflict between your edit and the target, then run the command again.
- Untracked files are ignored. Neither mode deletes untracked files. If the target commit contains a file with the same name as an untracked file, the reset may abort.
- Path arguments are not allowed. You cannot combine
--keepor--mergewith paths. Only the default mixed mode works on paths, as covered in the path reset chapter. - Staged edits can still vanish under --merge. If you use it outside a merge, check
git statusfirst. - History is still rewritten. The branch pointer moves just as with the other modes. The safety rules for shared branches apply fully, and
git reflogandORIG_HEADrecover the old position. - --keep is not stash. It does not save your edits aside. It leaves them untouched in the working directory.
Summary
--keepmoves the branch and updates files that differ betweenHEADand the target. It aborts if any of those files has local changes.--mergedoes the same but allows staged changes in differing files to be overwritten. It aborts only on unstaged changes there.--mergeis the mechanism behindgit merge --abort.- Both leave untracked files alone and refuse paths.
Exercise
You are on main with two commits you want to remove. You have an uncommitted edit to README.md, and neither commit touched README.md. Which mode removes the commits, makes the working directory match the older commit, and keeps the README.md edit? What happens if one of the removed commits also changed README.md?
Show answer
Run git reset --keep HEAD~2. It updates the files the two commits changed, and the unrelated README.md edit survives. If a removed commit also changed README.md, the file differs between HEAD and the target while having local changes. Git then aborts without changing anything, and you must commit or stash the edit first.
Exercise
You start a merge that produces conflicts, and you also have an unstaged edit in an unrelated file. Name two commands that abandon the merge and keep that edit. Which would you normally use?
Show answer
git merge --abort and git reset --merge. Both restore the pre-merge state and keep the unrelated unstaged edit. You would normally use git merge --abort, because its name states the intent.