AI Hints
Home›Git & GitHub Tutorial›Undoing Commits
Git & GitHub · Lesson 31 of 144

Moving Back with Reset

Git reset.

⏱ 2 min read▶ Video at 32:40🎓 Free
Watch this lesson in the Git & GitHub courseWatch on YouTube ↗Full Git & GitHub course

Git reset moves your branch back to an earlier commit, as if the later commits never happened. It's great for cleaning up work you haven't shared yet. It has three modes, soft, mixed, and hard.

Moving Back with Reset
From the video: Moving Back with Reset
  • Moves your branch back
  • Later commits disappear from the log
  • Three modes: soft, mixed, hard

Soft and mixed

Here, reset dash dash soft, head tilde one, which means one commit before head. The banner commit is gone from the log, but its change is still staged, ready to commit again. Now a plain reset, which is called mixed. Same thing, but the change is unstaged, sitting in your working folder.

git
git reset --soft HEAD~1
git log --oneline -n 2
f63a17a (HEAD -> main) Link to about page
ef9c947 Add basic styles
git status --short
M  index.html
git reset
Unstaged changes after reset:
M       index.html
git status --short
 M index.html

Hard

And now dash dash hard. This moves back, and throws the changes away completely. The log no longer has the banner commit. And status is clean. The banner is gone from the files too.

git
git reset --hard HEAD~1
HEAD is now at f63a17a Link to about page
git log --oneline -n 2
f63a17a (HEAD -> main) Link to about page
ef9c947 Add basic styles
git status
On branch main
nothing to commit, working tree clean

The three modes

Here are the three modes side by side. All of them move the branch back. Soft keeps the changes staged. Mixed, the default, keeps them in your files, unstaged. Hard throws them away. Use hard carefully.

ModeCommitChanges
--softremovedkept, staged
--mixed (default)removedkept, unstaged
--hardremovedthrown away

Summary

git reset moves your branch back. Soft keeps changes staged, mixed keeps them unstaged, hard throws them away. Next, when to use reset, and when to use revert.

Key takeaways
  • --soft: staged
  • --mixed: unstaged
  • --hard: gone
Next up: Reset or Revert? — choosing the right undo