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, that you know what a branch is, and that you read the previous chapter on --soft. You should also remember the three areas Git tracks: the HEAD commit, the index (also called the staging area), and the working directory.
What --mixed does
git reset --mixed <commit> does two things:
- It moves the current branch (and therefore HEAD) to
<commit>. - It resets the index to match that commit.
It leaves the working directory alone. Your files on disk keep their current contents.
--mixed is the default. These two commands are identical:
git reset --mixed HEAD~1
git reset HEAD~1The name "mixed" describes the middle position. --soft stops after step 1. --hard also overwrites the working directory. --mixed resets the index but stops short of touching your files.
| Mode | Moves branch | Resets index | Overwrites working directory |
|---|---|---|---|
| `--soft` | yes | no | no |
| `--mixed` (default) | yes | yes | no |
| `--hard` | yes | yes | yes |
The practical effect is that the changes from the commits you removed are not lost. They are no longer committed and no longer staged. They sit in the working directory as ordinary unstaged modifications, and you decide what to stage next.
Worked example
Start with the three-commit repository used throughout this tutorial. If you still have it from the earlier chapters, you can reuse it. Otherwise, create it again:
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 --onelineYou see three commits, with "third" at the top. Now remove the last commit with the default mode:
git reset HEAD~1
git statusThe output of git status looks like this:
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: file.txt
no changes added to commit (use "git add" and/or "git commit -a")Check what happened in each area:
- HEAD commit:
git log --onelinenow shows only "first" and "second". The branch moved back one commit. - Index: it now matches "second". Nothing is staged.
- Working directory:
file.txtstill containsone,two, andthree. The linethreeis now an uncommitted change.
You can confirm the file contents with cat file.txt, and see the leftover change with git diff. That diff compares the working directory against the index, and it shows the added three line.
Compare this with --soft from the previous chapter. After a soft reset the same change would appear under "Changes to be committed", because the index would still hold it. After a mixed reset it appears under "Changes not staged for commit".
Why the index reset matters
The only difference between --soft and --mixed is the index, but that difference changes what you do next.
After --soft, running git commit immediately recreates a commit with the same content. After --mixed, running git commit creates nothing, because nothing is staged. You must run git add first.
That extra step is useful. It lets you rebuild the commit more carefully:
- Stage only some of the files.
- Stage only some hunks of a file with
git add -p. - Split one large commit into several smaller ones.
For example, to split the commit you just removed into two commits:
git reset HEAD~1
git add -p
git commit -m "First part of the change"
git add -A
git commit -m "Second part of the change"git add -p walks you through each hunk (a contiguous block of changes) and asks whether to stage it. Whatever you stage goes into the first commit. The rest remain in the working directory until you stage and commit them in the second.
Resetting further back
--mixed works with any target, not only HEAD~1. If you reset three commits back, the changes from all three commits end up in the working directory as one combined set of unstaged changes.
git reset HEAD~2
git status
git diffHere git diff shows the net difference between the old index (now at "first") and your files. The individual commits are gone from the branch, and their changes are merged into one diff. Git does not remember which change belonged to which commit.
If you named a commit by hash or branch name instead of HEAD~n, the behavior is the same. Only the destination changes.
Edge cases
New files
Suppose the commit you remove had added a brand new file, notes.txt. After a mixed reset, the index no longer contains notes.txt, but the file is still on disk. Git now sees it as untracked. It appears under "Untracked files" in git status. You can stage it again with git add notes.txt.
Deleted files
Suppose the removed commit deleted old.txt. After a mixed reset, the index contains old.txt again, because the earlier commit had it. The working directory does not have the file. Git reports this as a deletion that is not staged:
Changes not staged for commit:
deleted: old.txtYou can restore the file with git restore old.txt, or stage the deletion with git rm old.txt.
Uncommitted changes you already had
If you had unstaged edits before the reset, --mixed leaves them in place. If you had staged edits before the reset, the reset removes them from the index, but the content stays in the working directory. In both cases you lose no file content. You lose only the staging information, meaning which changes you had selected.
Consider this sequence:
echo four >> file.txt
git add file.txt
git reset HEAD
git statusResetting to HEAD does not move the branch, because the target is where it already points. It only resets the index. file.txt goes back to being an unstaged modification. This is the same operation as unstaging, and the next chapter covers it with path arguments.
Staged and unstaged edits to the same file
If a file had some changes staged and other changes unstaged, a mixed reset merges both into one set of unstaged changes. You cannot tell afterward which hunks were staged. If that selection took effort, run git diff --cached > saved.patch before you reset.
Common errors
- Expecting the changes to disappear. Learners often assume
git resetdiscards work. In--mixedmode it does not. If you want the changes gone from the working directory, you need--hard, which the next chapter covers. - Running
git commitright after a mixed reset and getting "nothing added to commit". The index was reset, so you must rungit addfirst. Usegit commit -aas a shortcut. It stages changes to tracked files automatically. Be aware that-adoes not include new, untracked files. - Forgetting that new files become untracked. After resetting past a commit that added files, those files must be added again. A
git commit -awill skip them silently. - Resetting a branch others have pulled.
--mixedrewrites history on your local branch. If you already pushed the removed commits, a later push is rejected because the histories diverged. Do not force-push a shared branch to work around that. - Mistaking it for a recovery problem. A mixed reset keeps your file contents, so there is nothing to recover. Only the branch position changed. If you want the old commits back,
git reset ORIG_HEADreturns the branch to where it was, and a later chapter covers the reflog.
Checking your understanding
Use these checks to predict results before running a command:
- After
git reset HEAD~1, what doesgit diffshow? It shows the working directory against the index, which now equals the earlier commit. That is the removed commit's changes plus any edits you already had. - What does
git diff --cachedshow? Nothing. The index equals HEAD. - What does
git diff HEAD~0(orgit diff HEAD) show? The same content asgit diff, because the index and HEAD match.
Exercise
You made a commit that contains changes to a.txt and b.txt, and you want two separate commits, one per file. Which commands do this with a mixed reset, and why does a soft reset not work as well?
Show answer
Run git reset HEAD~1 to remove the commit and reset the index while keeping both files' changes in the working directory. Then run git add a.txt and git commit -m "Change a.txt", followed by git add b.txt and git commit -m "Change b.txt". A soft reset leaves both files staged, so you would first need to unstage one of them, which is the very thing the mixed reset already did for you.
Exercise
You run git reset HEAD~1 and then git status shows a file under "Untracked files" that you did not expect. What most likely happened, and how do you fix it?
Show answer
The commit you removed had added that file. The reset removed it from the index, but it remained in the working directory, so Git now sees it as untracked. Run git add <file> to stage it again, or delete it if you no longer want it.
Exercise
After a mixed reset, you have staged nothing and git commit -m "x" fails. What are two ways to get a commit that includes your modified tracked files?
Show answer
Stage the files first with git add <file> (or git add -A) and then commit, or run git commit -a -m "x", which stages changes to tracked files automatically. Neither approach picks up untracked files unless you add them explicitly.