Sub topic 7
--hard: reset the branch, index, and working directory
This chapter assumes you know the three areas Git tracks (the HEAD commit, the index, and the working directory). It also assumes you have read the --soft and --mixed chapters. You should be comfortable with git status and git log --oneline.
What --hard does
git reset --hard <commit> is the most far-reaching mode. It does three things:
- Moves the current branch (and HEAD) to the target commit.
- Resets the index to match that commit.
- Overwrites tracked files in the working directory to match that commit.
--soft stops after step 1. --mixed stops after step 2. --hard goes all the way, so it is the only mode that changes your files on disk.
| Mode | Moves branch | Resets index | Overwrites working directory |
|---|---|---|---|
| `--soft` | Yes | No | No |
| `--mixed` | Yes | Yes | No |
| `--hard` | Yes | Yes | Yes |
A worked example
Start from the three-commit repository built earlier in this tutorial:
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, and file.txt contains one, two, and three. Now run a hard reset back one commit:
git reset --hard HEAD~1
git statusGit reports that HEAD is now at the "second" commit. git status says the working tree is clean. If you run cat file.txt, you will see only one and two. The line three is gone from the file, not just from the history.
Compare this with --mixed. After a mixed reset, the line three would still be in file.txt as an unstaged change. With --hard, nothing is left over.
Uncommitted changes are lost
The commit you removed can usually be recovered, as the recovery chapter shows. Uncommitted changes to tracked files cannot. Suppose you edit file.txt and stage the edit, then run git reset --hard:
echo scratch >> file.txt
git add file.txt
git reset --hard HEADResetting to HEAD moves nothing, but it still resets the index and the working directory. The scratch line is discarded. Git never stored that content in a commit, so no reflog entry points to it. This is the main reason --hard is called destructive.
What --hard does not touch
--hard only overwrites files that Git tracks. Untracked files, meaning files that were never added, stay where they are. If you create notes.txt and run git reset --hard, notes.txt remains.
There is one related case to watch. Suppose the commit you reset away had added a file, and you reset to a commit before that file existed. Git removes the file from the working directory, because it was tracked in the old commit and does not exist in the new one.
Common mistakes
- Using
--hardwhen--mixedwould do. If you only want to undo a commit and keep the edits, use--softor--mixed. Reach for--hardonly when you want the changes gone. - Running it with uncommitted work. Commit or stash first if there is anything you might want back.
- Resetting a shared branch. After you push, other people may have the commits you removed. A hard reset followed by a force push rewrites history for them. The safety chapter covers this.
- Expecting it to delete untracked files. It does not. Removing those takes a separate command,
git clean.
Exercise
You have one commit that you no longer want, and your working directory also has an unstaged edit to a tracked file. You run git reset --hard HEAD~1. What happens to the commit and to the unstaged edit, and can you get each of them back?
Show answer
The branch moves back one commit, and the working directory is overwritten to match it. The removed commit can be recovered through the reflog (for example git reset --hard HEAD@{1}) or ORIG_HEAD. The unstaged edit is lost for good, because it was never committed and so no commit or reflog entry holds it.
Exercise
After git reset --hard, an untracked file notes.txt is still in your directory. Why?
Show answer
--hard only overwrites files that Git tracks. Untracked files are not in HEAD or the index, so reset does not touch them.