Git Reset
In short
Git reset is a Git command that moves the current branch back to an earlier commit and, depending on its mode, also resets the staging area and your files.
What is git reset?
git reset rewinds the current branch so it points at a different commit, usually an earlier one, which removes the later commits from that branch's history. It is mainly used to undo local commits that haven't been shared yet, or to unstage files. For example, git reset HEAD~1 moves the branch back one commit, where HEAD~1 means 'the commit before the current one'.
Three modes decide what happens to the changes from the commits you undo. --soft moves only the branch, leaving those changes staged and ready to commit again; --mixed, the default, also clears them from the staging area but keeps them in your working files; and --hard resets the staging area and working files as well, discarding every uncommitted change. Given a file path instead of a commit, as in git reset <file>, the command simply unstages that file.
Reset is like moving a bookmark back to an earlier page of a diary, where the mode decides whether the pages written since are kept as loose notes or thrown away. Commits you reset away are not destroyed at once: git reflog lists where the branch pointed before, so you can usually jump back with git reset --hard <old-hash> for weeks afterward. Uncommitted edits wiped out by --hard, however, were never saved in Git and cannot be recovered that way.
Git reset is most often confused with git revert. Reset rewrites history by moving the branch pointer, which is fine for local commits but causes trouble on a branch others have pulled, because their copies still contain the commits you removed. Revert leaves history intact and adds a new commit that undoes an old one, so it is the safe way to undo shared commits. Reset is also different from git restore, which only changes files and never moves a branch.
Key takeaways
git reset <commit>moves the current branch to another commit, removing later commits from it.--softkeeps the undone changes staged,--mixed(the default) keeps them unstaged, and--harddiscards them.git reset <file>unstages a file without changing its contents.- Reset rewrites history, so use it for local commits and
git revertfor shared ones. git reflogcan help you recover commits after a mistaken reset.
Example
# Undo the last commit but keep its changes staged
git reset --soft HEAD~1
# Undo the last commit and unstage its changes (the default, --mixed)
git reset HEAD~1
# Throw away the last commit AND all uncommitted changes (careful!)
git reset --hard HEAD~1
# Unstage a file without touching its contents
git reset src/app.ts
# Made a mistake? Find the previous position and go back to it
git reflog
git reset --hard HEAD@{1}Readers ask
What is the difference between git reset and git revert?
git reset moves a branch back to an earlier commit and drops the later ones from it, so it suits local work that hasn't been pushed. git revert creates a new commit that undoes an earlier one and keeps history intact, so it is safe on shared branches.
What is the difference between git reset --soft, --mixed, and --hard?
All three move the branch to the target commit. --soft keeps the undone changes staged, --mixed keeps them as unstaged edits, and --hard removes them from both the staging area and the working directory.
Can I undo a git reset --hard?
Committed work can usually be recovered: run git reflog, find the hash the branch pointed to before the reset, and reset back to it. Uncommitted changes discarded by --hard were never saved by Git and generally cannot be recovered.
Often compared
See also
- Git RevertVersion Control, p. 21Git revert is a Git command that undoes an earlier commit by creating a new commit with the opposite changes, leaving the existing project history intact.
- Staging AreaVersion Control, p. 37The staging area in Git is a holding zone between your files and the next commit, where you place exactly the changes you want that commit to include.
- CommitVersion Control, p. 5A commit is a saved snapshot of a project's files in Git, recorded with a unique ID, an author, a timestamp, and a message describing what changed.
- Git StashVersion Control, p. 22Git stash is a command that temporarily saves uncommitted changes and cleans the working directory, so you can switch tasks and restore the work later.
- Detached HEADVersion Control, p. 7A detached HEAD is a Git state in which you have checked out a specific commit instead of a branch, so any new commits you make don't belong to any branch.
- RebaseVersion Control, p. 33A rebase in Git replays a branch's commits on top of another commit, rewriting history to produce a cleaner, linear sequence of changes without merge commits.
Spotted a mistake or something missing on this page?Suggest an edit