TExcelTAD
Home › Git › Git Reset

Git Reset

git reset <commit> moves the current branch to another commit. Commits after that point are no longer on the branch. What happens to the staging area and your files depends on the mode:

Mode Branch pointer Staging area Working folder Use when
--soft moved unchanged unchanged you want to recommit the same changes differently
--mixed (default) moved reset to the commit unchanged you want to re-stage selectively
--hard moved reset reset: uncommitted changes are lost you want to throw everything away

All three examples below start from the same history of three commits and undo the last two.

--soft: uncommit, keep everything staged

bash · try it yourself
mkdir notes && cd notes && git init -q
echo "intro" > doc.txt && git add . && git commit -q -m "Intro"
echo "part 1" >> doc.txt && git commit -q -am "Part 1"
echo "part 2" >> doc.txt && git commit -q -am "Part 2"
git reset --soft HEAD~2
git log --oneline
git status -s
git commit -q -m "Parts 1 and 2"
git log --oneline
a76678f Intro
M  doc.txt
62deea0 Parts 1 and 2
a76678f Intro

The two commits were undone, but their combined change stayed staged (M in the left column), so a single new commit replaced them. That is a quick way to squash your latest commits.

--mixed: uncommit and unstage

bash · try it yourself
mkdir notes && cd notes && git init -q
echo "intro" > doc.txt && git add . && git commit -q -m "Intro"
echo "part 1" >> doc.txt && git commit -q -am "Part 1"
echo "part 2" >> doc.txt && git commit -q -am "Part 2"
git reset HEAD~2
git log --oneline
git status -s
cat doc.txt
Unstaged changes after reset:
M	doc.txt
a76678f Intro
 M doc.txt
intro
part 1
part 2

The changes are now unstaged (M in the right column) but still in the file. git reset without a commit (just git reset) unstages everything while keeping the branch where it is.

--hard: discard everything

bash · try it yourself
mkdir notes && cd notes && git init -q
echo "intro" > doc.txt && git add . && git commit -q -m "Intro"
echo "part 1" >> doc.txt && git commit -q -am "Part 1"
echo "part 2" >> doc.txt && git commit -q -am "Part 2"
echo "unsaved idea" >> doc.txt
git reset --hard HEAD~2
git log --oneline
git status -s
cat doc.txt
HEAD is now at a76678f Intro
a76678f Intro
intro

Both commits and the uncommitted "unsaved idea" are gone from the branch and the folder. The commits can still be found in the reflog for a while; the uncommitted line is lost for good.

Warning: git reset --hard is one of the few Git commands that can destroy work. Run git status first, and git stash anything you might want.

Resetting to the remote

A common use of --hard: your local main is a mess and you want it to match the server exactly:

bash · try it yourself
git init -q --bare ~/github/notes.git
mkdir notes && cd notes && git init -q
echo "intro" > doc.txt && git add . && git commit -q -m "Intro"
git remote add origin ~/github/notes.git && git push -q -u origin main
echo "experiment 1" >> doc.txt && git commit -q -am "Experiment 1"
echo "experiment 2" >> doc.txt && git commit -q -am "Experiment 2"
git status -sb | head -1
git fetch -q
git reset --hard origin/main
git status -sb | head -1
## main...origin/main [ahead 2]
HEAD is now at a76678f Intro
## main...origin/main

Resetting single files

With a path, git reset doesn't move the branch; it only copies the file from a commit into the staging area (it unstages). Modern Git has a clearer command for files, git restore:

Task Command
Unstage a file git restore --staged file (or git reset file)
Discard unstaged edits to a file git restore file
Get a file as it was in an older commit git restore --source=HEAD~3 file
bash · try it yourself
mkdir notes && cd notes && git init -q
echo "v1" > doc.txt && git add . && git commit -q -m "v1"
echo "v2" > doc.txt && git commit -q -am "v2"
echo "v3 draft" > doc.txt
git restore doc.txt
cat doc.txt
git restore --source=HEAD~1 doc.txt
cat doc.txt
git status -s
v2
v1
 M doc.txt

Reset and pushed commits

If you reset away commits that were already pushed, your branch is now behind the remote and a normal push is rejected. Forcing it (git push --force-with-lease) rewrites shared history. On shared branches use git revert instead.

Test yourself

Which reset mode keeps your changes staged?

  1. --soft
  2. --mixed
  3. --hard
  4. --keep

You run git reset --hard HEAD~1 with uncommitted edits in your files. What happens to those edits?

  1. They are stashed automatically
  2. They are discarded
  3. They are committed
  4. They move to a new branch

What does plain git reset (no options, no commit) do?

  1. Deletes the last commit
  2. Unstages everything, keeping the changes in your files
  3. Resets to the remote
  4. Nothing