git reset
Move the branch to another commit, for example to undo the last commits
What it does
git reset moves the current branch to the commit you name. HEAD~1 is the commit before the last one, HEAD~3 three back. Commits after it leave the branch, and the flag decides what happens to their changes.
--soft keeps the changes staged, ready to commit again, say as one commit instead of several. Without a flag (--mixed mode) the changes stay in your files but leave the index. --hard throws them away.
Careful. --hard erases both the undone commits and every uncommitted edit in tracked files. Undo commits that are already on the server with git revert: reset rewrites history, and a plain git push fails.
Syntax
git reset --soft <commit>
git reset <commit>
git reset --hard <commit>Examples
Undo the last commit but keep its changes staged
git reset --soft HEAD~1same as:
git reset --soft HEAD^reset— move the branch to another commit--soft— changes stay in the indexHEAD~1— one commit back
Throw away the last commit with all its changes
git reset --hard HEAD~1same as:
git reset --hard HEAD^reset— move the branch--hard— erase the changes — no way backHEAD~1— one commit back
Related commands
- git restore — Discard edits in a file or unstage it
- git revert — Undo a commit with a new commit, without rewriting history
- git commit — Save the staged changes to history as a new commit