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

Related commands

Practise · All commands