Reset, soft, mixed and hard
Move a branch back to an earlier commit and decide what happens to the index and your files: the three modes of git reset, tested on a real repository.
The three areas of GitUndoing uncommitted changesWhat a branch is
Reset, soft, mixed and hard
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `git reset <commit>` moves the current branch to `<commit>`, and its mode decides how far the change reaches into the staging area and working tree.
- `--soft` moves only the branch, leaving the undone changes staged, `--mixed` (the default) also resets the index, leaving them unstaged, and `--hard` also overwrites the working tree.
- `--hard` destroys uncommitted changes to tracked files, but leaves untracked files alone, and the discarded commits can usually be recovered through the reflog.
- `git reset <path>` with no commit moves nothing: it only unstages that file.
- Don't reset commits you have already pushed, because it rewrites shared history. Use `git revert` for those.
One command, three depths
Recall the three areas from the three areas of Git: your working tree, the index (staging area), and the commit that HEAD points to. git reset moves the current branch to another commit, and then decides how many of those areas to rewind to match.
Think of it as an undo with three depths. All three first move the branch. They differ in what else they reset.
| Mode | Moves branch | Resets index | Resets working tree |
|---|---|---|---|
--soft |
yes | no | no |
--mixed (default) |
yes | yes | no |
--hard |
yes | yes | yes |
I tested each on a three-commit jot repository (hashes will differ for you):
$ git log --oneline
c8c401e Add third note
de21b38 Add second note
5a7abc6 Add notes.txt
Soft: undo the commit, keep everything staged
$ git reset --soft HEAD~1
$ git status -sb
## main
M notes.txt
$ git log --oneline
de21b38 Add second note
5a7abc6 Add notes.txt
The commit is gone from the branch, and its changes sit in the index, staged and ready. This is the tool for “I committed too early” or “I want to squash my last three commits into one”: reset soft back three, then commit once.
Mixed: undo the commit and the staging
$ git reset HEAD~1
Unstaged changes after reset:
M notes.txt
$ git status -sb
## main
M notes.txt
No flag means --mixed. The changes are still in the file (note the second-column M), but unstaged. Use it when you want to re-pick what goes into the commit, for example with git add -p.
Hard: undo everything
$ echo "uncommitted" >> notes.txt
$ echo scratch > scratch.txt
$ git reset --hard HEAD~1
HEAD is now at de21b38 Add second note
$ git status -sb
## main
?? scratch.txt
$ cat notes.txt
one
two
Two things happened. The third commit is gone, and my uncommitted line uncommitted vanished from notes.txt too, with no recovery, because it was never committed and so Git never stored it. scratch.txt, an untracked file, survived, since reset --hard only touches files Git tracks. Treat --hard as the one irreversible mode for uncommitted work.
For committed work it is more forgiving. Git saves the previous tip in ORIG_HEAD:
$ git rev-parse --short ORIG_HEAD
c8c401e
$ git reset --hard ORIG_HEAD
HEAD is now at c8c401e Add third note
That undid the undo. When ORIG_HEAD has been overwritten, the reflog remembers.
Reset with a path moves nothing
$ git add notes.txt
$ git status -sb
M notes.txt
$ git reset notes.txt
Unstaged changes after reset:
M notes.txt
When you give a path, Git doesn’t move the branch. It copies that path from HEAD to the index, which is unstaging. This is the same operation as git restore --staged notes.txt. There is no --hard for paths.
What not to reset
git reset rewrites history on your branch. If you’ve pushed those commits, anyone who pulled them now has commits your branch doesn’t, and you’d need a force push to publish the reset. For published commits, use git revert, which adds a new commit instead.
Which one to reach for:
- Unstage a file:
git restore --staged <file>orgit reset <file>. - Redo the last commit:
git commit --amend, orgit reset --soft HEAD~1. - Throw away all local changes:
git reset --hard, knowing it can’t be undone for uncommitted work.
What the video covers
GitKraken’s “Git Tutorial #34: How to Use Git Reset Soft, Mixed, and Hard” is about ten minutes and was published around 2022. From its title it walks through the three modes. GitKraken makes a Git GUI, so the video may show its client alongside the command line. I judge English from the title, description and channel only, and I could not read a transcript. A visual walkthrough from “A shot of code” is linked as a runner-up.
Where this fits
Reset moves your branch. The safer way to undo something already published is to add a commit that reverses it: reverting a commit.
Check yourself
You committed too early and want the changes back in the staging area, ready to recommit. Which reset?
`git reset --soft HEAD~1`. It removes the commit and leaves its changes staged.
What is the difference between `git reset --mixed HEAD~1` and `--hard HEAD~1`?
Both move the branch back one commit. Mixed keeps the changes in your files as unstaged edits, and hard discards them from your files too.
Does `git reset --hard` delete untracked files?
No. It only touches tracked files. Untracked files stay, which `git clean` is for.
What does `git reset notes.txt` do?
It unstages `notes.txt` by copying it from `HEAD` back into the index. The branch and the file contents don't change.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.