WeCodeAcademy

Rewinding with git reset

git reset moves your branch back to an earlier commit. It has three modes, which differ in what happens to your changes:

  • --soft: undo the commit, keep the changes staged.
  • (default) --mixed: undo the commit, keep the changes but unstaged.
  • --hard: undo the commit and throw the changes away.

HEAD~1 means "one commit before the current one".

Examplebash
Open in editor »
/ $

The commit "Three" is gone from the log, but its change is still staged. Recommit it, then try the default mode, which unstages the change.

Examplebash
Open in editor »
/ $

Finally --hard, the dangerous one. It rewinds the files too, so the work in them is lost.

Examplebash
Open in editor »
/ $

git reset is safe for commits you have not pushed. Never use it to rewrite commits other people already have.

Quick check

Which command undoes the last commit and throws its changes away?

Your turn: write the code

Make two commits, then throw the last one away with a hard reset, and print how many commits remain.

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.