Reset, soft, mixed and hard

Intermediate · 10 min

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.

Before thisThe three areas of GitUndoing uncommitted changesWhat a branch is

GitKraken · 10 min

Reset, soft, mixed and hard

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by GitKrakenReport a problem with this video
Not started
Set this lesson's status

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> or git reset <file>.
  • Redo the last commit: git commit --amend, or git 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

  1. 01You 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.

  2. 02What 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.

  3. 03Does `git reset --hard` delete untracked files?

    No. It only touches tracked files. Untracked files stay, which `git clean` is for.

  4. 04What 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

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark