Git Cheat Sheet · Rewrite history

Go back to an earlier commit

git reset --soft | --mixed | --hard <commit>

git reset <commit> moves the current branch to an older commit. The option decides what happens to the changes of the removed commits: --soft keeps them staged, --mixed (the default) keeps them in the files, --hard erases them.

What it does

Moves the current branch to another commit. Soft keeps everything staged, mixed keeps modified files, hard erases everything.

A use case

You committed too early: a soft reset to the previous commit gives you back the changes, ready to be recommitted.

Warning: A hard reset deletes local changes and makes the following commits unreachable.

On the command line

git reset --soft HEAD~1
Undoes the last commit; its changes stay staged.
git reset HEAD~1
Undoes the last commit; its changes stay in the files, unstaged.
git reset --hard HEAD~1
Undoes the last commit and erases its changes.
git reset --hard origin/main
Aligns the branch with the remote, abandoning all local work.
git reset --hard ORIG_HEAD
Undoes a reset, merge or pull you have just done.

In Merathon

  1. Right-click the target commit in the graph → "Reset <branch> here — soft", "mixed" or "hard".

Frequently asked questions

How do I get commits back after a git reset --hard?

git reflog lists HEAD's past positions; git reset --hard <hash> goes back to one. Changes that were never committed cannot be recovered.

Can I reset commits already pushed?

Only on your own branch, with a force push afterwards. On a shared branch, use git revert.

See also