Git reset
The `git reset` command can move your current branch to another commit or take changes out of the next commit without deleting your edits. Its `--hard` mode can also overwrite your files.
Also known as: git reset, reset --hard
What git reset does
Suppose your last local commit contains two unrelated changes. You want to split it into two commits, but you don't want to redo the work. A reset can move the branch back one commit while leaving your edited files in place. The same command can also unstage a single file without moving the branch at all.
What each mode changes
To predict a reset, keep track of three things: HEAD, the index, and the working tree. HEAD normally points to the current commit through the branch you have checked out. The index, also called the staging area, holds what will go into the next commit. The working tree is the set of files on disk. Running git add copies changes into the index; editing a file again doesn't automatically update what you staged.
When you give git reset a target commit and no file paths, the mode determines what gets updated:
| Mode | Branch | Index | Working tree |
|---|---|---|---|
--soft |
Moves to the target | Unchanged | Unchanged |
--mixed (default) |
Moves to the target | Matches the target | Unchanged |
--hard |
Moves to the target | Matches the target | Tracked files match the target |
In detached HEAD state, the command moves HEAD directly instead of moving a branch. With --hard, uncommitted changes to tracked files can be lost. Untracked files generally stay put, but Git can remove them if they are in the way of writing tracked files from the target commit.
Example: redo a local commit
Start with a clean working tree and index, and assume the last commit hasn't been shared:
git reset --mixed HEAD~1
git status
git diff
HEAD~1 means the current commit's first parent. After the reset, the branch points to that parent and the index matches it. Your files haven't changed. Edits to existing tracked files now appear as unstaged changes, and any file introduced by the old commit is now untracked. Use git status to spot both: git diff on its own won't show untracked file contents. You can then stage and commit one set of changes at a time.
Common mistakes and interview questions
A typical interview question is: "How do you unstage one file without losing your edits?" Use git reset -- README.md or git restore --staged README.md. Both replace the index entry for that path with the version from HEAD, leaving the branch and the file on disk alone. This is a different form of reset from moving the branch to an earlier commit.
Another common question is when to use git revert instead. A reset is local: it doesn't undo a push or move the remote branch. To undo a commit on a shared branch, prefer git revert, which records the inverse change in a new commit and preserves existing history.
Before using --hard, run git status and save any edits you need. If you reset to the wrong commit, git reflog can often help you find the old branch tip. It records changes to references, not a history of your unsaved file edits.