Sub topic 13
Less common modes: --keep and --merge
This chapter assumes you know --soft, --mixed, and --hard, and the three areas they touch: the HEAD commit, the index, and the working directory. Two more modes, --keep and --merge, work like --hard but refuse to destroy uncommitted work. You will rarely type them, but you should know what they do.
The problem they solve
git reset --hard overwrites tracked files with no warning. If you have uncommitted edits, they are gone. --keep and --merge also move the branch and update the index and working directory. The difference is that they only touch files that must change, and they abort instead of overwriting local edits.
--keep: reset, but protect local changes
--keep moves the branch to the target commit and updates only the files that differ between the current HEAD and the target. Your uncommitted changes to other files stay in place. If a file that must change also has local changes, Git aborts the whole reset and leaves everything untouched.
The following script creates two files and a second commit. It then makes an uncommitted edit to the file the commit did not touch.
git init keep-demo && cd keep-demo
echo one > file.txt
echo notes > notes.txt
git add file.txt notes.txt
git commit -m "first"
echo two >> file.txt
git commit -am "second"
echo "work in progress" >> notes.txt
git reset --keep HEAD~1
git status
cat file.txt notes.txtfile.txt goes back to one, because it differs between HEAD and HEAD~1. notes.txt still contains work in progress, because it is the same in both commits.
Now edit file.txt instead, and run the reset again from a fresh "second" commit. Git stops with an error, because the reset would overwrite your edit. git reset --hard HEAD~1 in the same situation would discard the edit without asking.
--merge: reset out of a failed merge
--merge is built for backing out of a merge. It resets the index and updates files that differ between the target and HEAD. It keeps changes that are in the working directory but not in the index. If a file that differs between the target and the index has unstaged changes, the reset aborts.
The usual use is a merge that stopped on conflicts:
git init merge-demo && cd merge-demo
echo base > file.txt
git add file.txt
git commit -m "base"
git switch -c feature
echo feature > file.txt
git commit -am "feature change"
git switch -
echo main > file.txt
git commit -am "main change"
git merge feature
git reset --merge
git statusThe git merge command reports a conflict in file.txt. git reset --merge then abandons the merge and returns the branch, index, and file to the state before it. When a merge is in progress, this is what git merge --abort does.
Comparing the three destructive-looking modes
| Mode | Moves branch | Resets index | Updates working files | Uncommitted edits |
|---|---|---|---|---|
| `--hard` | Yes | Yes | All tracked files | Discarded |
| `--keep` | Yes | Yes | Only files that differ between HEAD and target | Kept, or abort if they overlap |
| `--merge` | Yes | Yes | Only files that differ between HEAD and target | Unstaged edits kept, or abort if they overlap |
Common errors
- Expecting
--keepto always succeed. It aborts when your local edits touch a file the reset must change. Commit or stash those edits first, then retry. - Using
--mergeon ordinary work. It is designed for a merge in progress. For an everyday undo,--mixedor--keepis clearer. - Assuming an abort leaves a half-finished state. Both modes check first and change nothing when they refuse.
Exercise
You are on a feature branch with uncommitted edits to notes.txt. You want to move the branch back one commit, and that commit changed only file.txt. Which mode moves the branch and keeps your edits, and what happens if the commit had also changed notes.txt?
Show answer
Use git reset --keep HEAD~1. notes.txt is the same in both commits, so the edits stay. If the commit had also changed notes.txt, Git would abort the reset and leave everything as it was, because the reset would overwrite your edits.
Exercise
What is the practical difference between git reset --hard and git reset --merge after a merge stops on conflicts?
Show answer
--merge is meant for this case. It undoes the merge and keeps unstaged changes that are unrelated to the merge, aborting if they overlap. --hard resets every tracked file and discards all uncommitted changes.