Sub topic 7
--hard: reset the branch, index, and working directory
git reset --hard is the widest of the three reset modes. It moves the current branch, makes the index match the new commit, and overwrites tracked files in the working directory to match as well. It is also the only common reset mode that can destroy uncommitted work.
This chapter assumes you know the three areas Git tracks (the commit that HEAD points to, the index, and the working directory), and that you have seen --soft and --mixed. It also assumes you are comfortable with git status and git log --oneline.
What --hard does
A reset runs in up to three steps. --soft performs only the first. --mixed performs the first two. --hard performs all three.
- Move the current branch (and so HEAD) to the target commit.
- Reset the index to match the target commit.
- Reset the tracked files in the working directory to match the target commit.
After git reset --hard <commit>, the three areas hold the same content. git status reports a clean tree, unless untracked files are present.
Worked example
Start with the three-commit repository from earlier in the tutorial. If you no longer have it, recreate it:
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 --onelineThe log shows three commits, with "third" at the top. Now run the hard reset:
git reset --hard HEAD~1
git statusGit prints a line such as HEAD is now at 1a2b3c4 second. The branch now points to "second". The index matches "second", and file.txt contains only the lines one and two. The line three is gone from the working directory. git status reports nothing to commit, working tree clean.
Compare this with the other modes after the same HEAD~1 reset:
| Mode | Branch moves | Index reset | Working directory reset | Where the "three" change ends up |
|---|---|---|---|---|
| `--soft` | Yes | No | No | Staged |
| `--mixed` | Yes | Yes | No | Unstaged modification |
| `--hard` | Yes | Yes | Yes | Not in any area |
With --hard, the removed commit's changes are no longer in the index or the working directory. The commit object itself still exists in the repository for a while, which matters for recovery later in this chapter.
Uncommitted changes are discarded
The commit-level effect is recoverable. The effect on uncommitted work is not. If you have edited a tracked file and not committed the edit, git reset --hard overwrites it with the version from the target commit. This is true even when you reset to HEAD, which moves nothing:
echo scratch >> file.txt
git add file.txt
echo more >> file.txt
git reset --hard HEAD
git statusBoth the staged line scratch and the unstaged line more are gone. Resetting to HEAD is a common way to say "throw away everything I have changed since the last commit."
The three cases differ in how much Git remembers:
- Committed changes are stored in commit objects. The reflog still records where the branch used to point, so you can get them back.
- Staged changes were written into the object database as blob objects when you ran
git add. Those blobs are not reachable from any commit, but they may still exist. You can list them withgit fsck --lost-found, then inspect each withgit show <hash>. This is slow and the blobs have no file names, so treat it as a last resort. - Unstaged changes to tracked files were never stored by Git. Once overwritten, they are gone. Only an editor's local history or a file system backup can help.
Untracked files are left alone
git reset --hard only touches files that Git tracks, meaning files that are in the index or in the target commit. Untracked files are not modified. Consider this case:
echo notes > notes.txt
git reset --hard HEAD~1
lsnotes.txt is still there. Git does not delete files it has never recorded. To remove untracked files you need git clean, which is a separate and equally unrecoverable operation. Run git clean -n first to preview what it would delete.
There is one edge case. Suppose an untracked file has the same path as a file that exists in the target commit. A hard reset overwrites it, because Git treats the target commit as the source of truth for that path. Ignored files (matched by .gitignore) are also left alone unless a tracked file occupies the same path.
Files that were added by the removed commit are different. If the commit you reset away from created new.txt, then git reset --hard deletes new.txt from the working directory, because it was tracked in the old HEAD and is absent from the target.
Resetting to a different target
The target does not have to be a relative reference such as HEAD~1. Any commit-ish works:
git reset --hard 1a2b3c4
git reset --hard origin/main
git reset --hard main~2The origin/main form is common when a local branch has diverged and you want to match the remote exactly. Run git fetch first, because origin/main is only a remembered copy of the remote branch until you update it. Any local commits not on origin/main will be dropped from the branch.
Hard reset during a merge
git reset --hard is also the usual way to abandon a merge that has stopped on conflicts. A merge in progress has conflicted entries in the index and conflict markers in the working directory. Running this returns everything to the state before the merge started:
git reset --hard HEADOn modern Git you can use git merge --abort for the same purpose. It is safer because it refuses to act when no merge is in progress. Both commands discard any uncommitted changes you made before starting the merge.
Recovering from a hard reset
Because the old commits still exist as objects, you can usually recover them. Two tools help.
ORIG_HEAD is a reference that Git sets before a risky operation such as a reset or merge. It holds the position HEAD had before the operation:
git reset --hard ORIG_HEADThis returns the branch to where it was before the most recent reset. Another reset, merge, or rebase overwrites ORIG_HEAD, so use it immediately.
The reflog is a longer record of where HEAD has pointed:
git reflog
git reset --hard HEAD@{1}git reflog lists entries with the newest first. HEAD@{1} means "where HEAD was one move ago". After a reset, that is the commit you reset away from. If you have done other work since, read the reflog output and pick the entry by its hash instead of counting.
git reflog
git reset --hard 9f8e7d6Recovery has limits:
- It restores committed work only. A hard reset to recover a commit does not bring back uncommitted edits that were discarded earlier.
- Reflog entries expire. By default, unreachable entries are kept for 30 days and reachable ones for 90. After that,
git gccan delete the commits. - The reflog is local. A fresh clone has no history of what you did in another copy of the repository.
- Running a hard reset to recover will itself overwrite uncommitted changes. Commit or stash first.
If you want to look before you restore, create a branch at the old position instead. This does not touch your working directory:
git branch rescue 9f8e7d6Common mistakes
- Resetting with local changes present. Run
git statusfirst. If anything you want to keep appears, commit it or rungit stashbefore the reset. - Resetting a shared branch. If you have pushed the commits you are removing, a hard reset makes your branch disagree with the remote. Pushing then requires a force push, which rewrites history for everyone who pulled it. Use
git reverton shared branches instead. - Expecting untracked files to disappear. They stay. If a stray file remains after the reset,
git cleanis the tool, with-nfirst. - Confusing
HEAD~1andHEAD@{1}.HEAD~1is the parent of the current commit.HEAD@{1}is the previous position in the reflog. After a reset they usually point to different commits. - Using it to unstage one file.
git reset --hard -- file.txtis not allowed. Hard mode cannot be combined with paths. Usegit reset -- file.txtto unstage orgit restore file.txtto discard working directory edits.
A safe habit
A cheap safety net is to mark the current position before a risky reset:
git branch backup-before-reset
git reset --hard HEAD~3If the result is wrong, git reset --hard backup-before-reset returns the branch. Delete the backup with git branch -D backup-before-reset when you no longer need it.
Exercise
You have three commits, and file.txt has an uncommitted edit that you have not staged. You run git reset --hard HEAD~1. Which of these is lost permanently, and which can you get back? (a) the third commit's changes, (b) the uncommitted edit.
Show answer
The uncommitted edit (b) is lost permanently, because Git never stored it and the hard reset overwrote the file. The third commit's changes (a) can be recovered with git reset --hard ORIG_HEAD or by finding the old commit in git reflog, because the commit object still exists.
Exercise
After a hard reset you run ls and see an untracked file notes.txt you created earlier. Why did it survive, and what command would remove it?
Show answer
git reset --hard only changes files Git tracks, so it never touched notes.txt. git clean removes untracked files. Run git clean -n first to preview, then git clean -f to delete.