Menu
Coddy logo textTech

Reset A Branch

Part of the Version Control section of Coddy's Terminal journey — lesson 44 of 58.

git reset moves the current branch pointer to a different commit. The most common use is to undo recent commits you have not shared yet.

git reset --hard HEAD~1

This moves main back one commit and overwrites the working directory to match. The commit you skipped is no longer reachable from your branch (Git holds onto it for a while in case you change your mind, but expect it to disappear).

Three flavors of reset exist:

  • --soft: move the branch pointer; keep staged changes
  • --mixed (default): move the pointer; unstage but keep working files
  • --hard: move the pointer; throw away working file changes too

--hard is the most destructive form. It is great for cleaning up local mistakes, but never use it on commits other people have already pulled.

challenge icon

Challenge

Medium

The folder contains doc.md. Initialize the repo and create three commits in order:

  1. Add doc.md with message Add doc.
  2. Append second to doc.md, commit with message Second draft.
  3. Append third to doc.md, commit with message Third draft.

Hard-reset the branch back to the very first commit using HEAD~2. Print the contents of doc.md.

Cheat sheet

git reset moves the current branch pointer to a different commit. Three flavors:

  • --soft: move pointer; keep staged changes
  • --mixed (default): move pointer; unstage but keep working files
  • --hard: move pointer; discard working file changes
git reset --hard HEAD~1  # undo last commit, discard changes
git reset --hard HEAD~2  # undo last 2 commits

Warning: Never use --hard on commits others have already pulled.

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Version Control