Git Reset: Soft, Mixed and Hard

Updated · 12 min read
mediumGit 2.53gitgit-resetreflogundo-commitinterview

A bookmark marks where you are in a book. Move it back three chapters, and the book itself is unchanged, but "where you are" is now earlier. What happens to the notes you made in those three chapters is a separate question: you could keep them on your desk, put them in a pile to sort, or throw them away.

A branch is a bookmark on a commit, and git reset moves it. Move main back one commit, and that commit is no longer part of the branch's history. The mode answers the second question, what happens to the changes from the commits you stepped back over: --soft keeps them staged, --mixed keeps them in your files but unstaged, and --hard throws them away.

In this lesson, you will use all three modes, learn to recover from a reset you regret, and see why git reset is only for commits you haven't shared.

The Starting Point

Navika's project has four commits. The last one added the hostel fee:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
git add README.md
git commit --quiet -m "Add project README"
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fees"
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the late fee"
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add hostel"
git log --oneline
Output
641eff7 (HEAD -> main) Add hostel
4c2a088 Add the late fee
4009644 Add the term fees
dd34d8a Add project README

--soft: Undo the Commit, Keep the Changes Staged

The last message, "Add hostel", is too short. A soft reset takes back the commit, and leaves its changes staged, ready to commit again:

Terminal
git reset --soft HEAD~1
git log --oneline
git status -s
Output
4c2a088 (HEAD -> main) Add the late fee
4009644 Add the term fees
dd34d8a Add project README
M  fees.txt

main now points to "Add the late fee". The hostel fee commit is no longer in the history, but its change is still there, staged (M ). Navika commits it again with a better message:

Terminal
git commit --quiet -m "Add the hostel fee"
git log --oneline
Output
ec97acb (HEAD -> main) Add the hostel fee
4c2a088 Add the late fee
4009644 Add the term fees
dd34d8a Add project README

The new commit has a new ID, ec97acb, not the old 641eff7. It is a new commit with a new message and time, not the old one put back.

--soft is also the easy way to join your last few commits into one: git reset --soft HEAD~3 followed by one git commit.

--mixed: Undo the Commit, Keep the Changes Unstaged

--mixed is the default, so git reset HEAD~1 means the same. The commit is taken back and its changes stay in your files, but they are not staged:

Terminal
git reset HEAD~1
git log --oneline
git status -s
Output
Unstaged changes after reset:
M	fees.txt
4c2a088 (HEAD -> main) Add the late fee
4009644 Add the term fees
dd34d8a Add project README
 M fees.txt

Git lists the files that now have unstaged changes, under Unstaged changes after reset:. The status is M this time. This is the mode to use when you want to rethink the commit: change the files, stage only some of them, and make one or several new commits. Navika commits it again as it was:

Terminal
git commit --quiet -am "Add the hostel fee"
git log --oneline
Output
59152da (HEAD -> main) Add the hostel fee
4c2a088 Add the late fee
4009644 Add the term fees
dd34d8a Add project README

--hard: Undo the Commit and Throw Away the Changes

--hard moves the branch and makes the staging area and your files match the commit it moves to. The changes from the commits you stepped back over are removed from your folder:

Terminal
git reset --hard HEAD~1
cat fees.txt
git status -s
Output
HEAD is now at 4c2a088 Add the late fee
Term 1 fee: $12,000
Late fee: $250

Git confirms with HEAD is now at, the new commit. The hostel line has gone from fees.txt, and the status is clean.

What --soft, --mixed and --hard change
ModeBranch movesStaging areaWorking treeThe undone changes are
--softYesUnchangedUnchangedStaged
--mixed (default)YesReset to the commitUnchangedIn your files, unstaged
--hardYesReset to the commitReset to the commitGone from your files

Syntax

Terminal
git reset [--soft | --mixed | --hard] [<commit>]
git reset <file>
CommandIt is used to
git reset --soft HEAD~1Undo the last commit, keep its changes staged
git reset HEAD~1Undo the last commit, keep its changes unstaged
git reset --hard HEAD~1Undo the last commit and throw its changes away
git reset --hardThrow away all uncommitted changes (no commit given means HEAD)
git reset <file>Unstage a file, the older form of git restore --staged

Get Back from a Reset

Navika didn't mean to lose the hostel fee. The commit is no longer on main, but Git hasn't deleted it. The reflog, Git's record of where HEAD has been, still knows it:

Terminal
git reflog -3
Output
4c2a088 (HEAD -> main) HEAD@{0}: reset: moving to HEAD~1
59152da HEAD@{1}: commit: Add the hostel fee
4c2a088 (HEAD -> main) HEAD@{2}: reset: moving to HEAD~1

HEAD@{1} is where HEAD was just before the hard reset: the hostel fee commit. Resetting to it moves main forward again:

Terminal
git reset --hard HEAD@{1}
git log --oneline
cat fees.txt
Output
HEAD is now at 59152da Add the hostel fee
59152da (HEAD -> main) Add the hostel fee
4c2a088 Add the late fee
4009644 Add the term fees
dd34d8a Add project README
Term 1 fee: $12,000
Late fee: $250
Hostel fee: $9,000

The commit and its change are back. This works because a commit you reset away stays in the repository for a while (at least 30 days by default) before Git cleans it up. The lessons on recovering lost work cover the reflog in detail.

Never Reset Commits You Have Pushed

git reset rewrites the history of your branch. If you have pushed those commits, your teammates already have them. After your reset, your history and theirs disagree: Git refuses your next ordinary push, and forcing it removes commits other people may have built on.

For commits that are already shared, use git revert, which undoes a commit by adding a new commit, without rewriting anything. It has its own lesson next.

The commit isUndo it with
Only on your computergit reset (any mode)
Pushed and sharedgit revert

In Practice

"I committed to the wrong branch." Navika commits the exam fee on main, then remembers that the team wanted it on its own branch first. A branch is created where she is now, so the commit is safe on it, and then main is moved back:

Terminal
echo 'Exam fee: $800' >> fees.txt
git commit --quiet -am "Add the exam fee"
git branch exam-fee
git reset --quiet --hard HEAD~1
git log --oneline -2 main
git log --oneline -2 exam-fee
Output
59152da (HEAD -> main) Add the hostel fee
4c2a088 Add the late fee
ce564e8 (exam-fee) Add the exam fee
59152da (HEAD -> main) Add the hostel fee

main is back at the hostel fee, and the exam fee commit lives on exam-fee, ready to be switched to and finished. Branches have their own lessons, and a later lesson covers the case where the right branch already exists.

Common Mistakes

Using --hard when you meant --soft or --mixed. If you want to keep the changes, don't use --hard. If it already happened and the work was committed, use git reflog to get it back.

Resetting with uncommitted work in your folder. --hard overwrites it. Check git status first.

Resetting pushed commits. Use git revert for shared history.

Counting wrong with HEAD~n. git reset HEAD~2 takes back two commits, not the second one only. Check with git log --oneline first, or reset to a commit ID.

Thinking git reset <file> changes commits. With a path, reset only unstages. It never moves the branch.

Under the Hood

A reset to a commit runs up to three steps, and the mode decides where it stops:

  1. Move the branch that HEAD points to so it points to the commit. (--soft stops here.)
  2. Make the staging area match that commit's snapshot. (--mixed stops here.)
  3. Make the working tree match the staging area. (--hard does all three.)

Step 1 only rewrites a 41-byte file such as .git/refs/heads/main, which is why a reset is instant, and why the old commit is still there afterward: nothing deleted it, it just isn't on the branch anymore. Git also writes the old position to ORIG_HEAD, so right after a reset, git reset --hard ORIG_HEAD undoes it as well.

Interview Corner

What is the difference between git reset --soft, --mixed and --hard?

All three move the current branch to the given commit. --soft changes nothing else, so the undone changes are staged. --mixed, the default, also resets the staging area, so the changes are in the working tree, unstaged. --hard resets the staging area and the working tree, so the changes are gone, along with any uncommitted work.

How do you undo the last commit but keep its changes?

git reset --soft HEAD~1 keeps them staged, and git reset HEAD~1 keeps them unstaged. If the commit was already pushed, use git revert instead.

You ran git reset --hard by mistake. Can you recover?

Committed work, yes: find the earlier position with git reflog and run git reset --hard HEAD@{n} (or ORIG_HEAD right after the reset). Uncommitted changes that were overwritten can't be recovered by Git.

Why shouldn't you reset commits that are already pushed?

Reset rewrites the branch's history. Others already have the old commits, so the histories split: a normal push is rejected, and a force push removes commits that teammates may have built on. git revert undoes a pushed commit safely by adding a new commit.

What does git reset <file> do?

It unstages the file: it copies the version from HEAD into the staging area and leaves the working tree alone. It doesn't move any branch. The modern form is git restore --staged <file>.

Practice

Challenge: two commits back. In the same repository, you are on main with the hostel fee as the newest commit. Add the line Run the app. to README.md and commit it. Then run git reset HEAD~2 and print the one-line log and the short status.

What does Git print? Before you open the solution, decide which commit main ends on, and which files show changes and in which column.

Show solution
Terminal
echo "Run the app." >> README.md
git commit --quiet -am "Explain how to run the app"
git reset --quiet HEAD~2
git log --oneline
git status -s
Output
4c2a088 (HEAD -> main) Add the late fee
4009644 Add the term fees
dd34d8a Add project README
 M README.md
 M fees.txt

HEAD~2 is two commits back from the README commit: "Add the late fee". The mode is the default, --mixed, so the changes of both undone commits, the hostel fee and the README line, are in the files but unstaged: M for both. --quiet only hides the list of unstaged files that git reset prints.

Checkpoint. In a practice repository, make three commits. Undo the last one with each mode in turn (recommitting in between), and run git status -s and git log --oneline after each. Then do a --hard reset and get the commit back with git reflog.

Quick check: Git reset

4 questions
1After git reset --soft HEAD~1, where are the changes from the undone commit?
2What is the default mode of git reset <commit>?
3Which work can git reflog help you get back after git reset --hard?
4You need to undo a commit that teammates have already pulled. What do you use?

Key Takeaways

Quick reference

CommandIt is used to
git reset --soft HEAD~1Undo the last commit, keep changes staged
git reset HEAD~1Undo the last commit, keep changes unstaged
git reset --hard HEAD~1Undo the last commit and its changes
git reset --hardThrow away all uncommitted changes
git reflogSee where HEAD has been
git reset --hard HEAD@{1}Go back to where HEAD was before the last move
git reset --hard ORIG_HEADUndo the reset you just did

Was this tutorial helpful?

Lesson 24 of 106