Sub topic 6
--mixed: move the branch and reset the index
This chapter covers the default mode of git reset. It assumes you can use init, add, commit, status, and log, and that you know the three areas from earlier chapters: the HEAD commit, the index (staging area), and the working directory. It also assumes you have seen --soft, which moves only the branch.
What --mixed does
git reset --mixed <commit> does two things:
- It moves the current branch (and HEAD with it) to
<commit>. - It resets the index to match that commit.
It leaves the working directory alone. The changes from the removed commits are not lost. They stay in your files as unstaged modifications.
--mixed is the default. git reset HEAD~1 and git reset --mixed HEAD~1 do the same thing.
| Mode | Moves branch | Resets index | Changes working directory |
|---|---|---|---|
| `--soft` | yes | no | no |
| `--mixed` | yes | yes | no |
| `--hard` | yes | yes | yes |
Worked example
Start from a repository with three commits.
git init demo && cd demo
echo one > file.txt
git add file.txt
git commit -m "first"
echo two >> file.txt
git commit -am "second"
echo three >> file.txt
git commit -am "third"
git log --onelineThe log shows third, second, and first. Now run a mixed reset back one commit.
git reset HEAD~1
git statusgit status reports file.txt under "Changes not staged for commit". The branch now points at second. The index matches second, so the line three is no longer staged. The file on disk still contains three, so the work is intact.
Compare this with --soft. After git reset --soft HEAD~1, the same change appears under "Changes to be committed", because the index was not touched. With --mixed, you must run git add again before you can commit the change.
When to use it
Use --mixed when you want to redo a commit from scratch. Typical cases:
- You committed too much and want to split the work into several smaller commits.
- You committed the wrong set of files and want to choose again.
- You want to see the combined changes of recent commits as plain edits before staging them selectively.
After a mixed reset, stage in pieces with git add -p or git add <file>, then commit each piece.
Common errors
Expecting staged changes. People often run git reset HEAD~1 and then git commit, and Git reports "no changes added to commit". The index was reset, so you must stage first. If you want the changes to stay staged, use --soft.
Missing new files. A file created in the removed commit is no longer tracked after a mixed reset. It becomes untracked, and git status lists it under "Untracked files". It still exists on disk. You must git add it again.
Thinking it is destructive. --mixed does not change your files. The removed commit is still reachable through the reflog, which a later chapter covers. Only --hard overwrites the working directory.
Resetting shared history. If you already pushed the removed commits, a mixed reset on your local branch makes it diverge from the remote. Do this only on commits that are local to you.
Check your understanding
Exercise
You have three commits. You run git reset HEAD~2, then git status. Where does git status show the changes from the two removed commits, and what must you do before you can commit them again? Assume the second and third commits modified files that already existed.
Show answer
They appear under "Changes not staged for commit", because --mixed is the default and resets the index while leaving the working directory as it was. Before committing, you must stage them with git add (for example git add -p or git add <file>).
Exercise
A commit added a new file notes.txt. After git reset HEAD~1, where does notes.txt appear in git status, and is its content lost?
Show answer
It appears under "Untracked files". Its content is not lost, because the working directory is not changed. Git simply no longer tracks it, since the reset index does not contain it.