Lesson 3 of 6 in Undoing Things

Moving a branch back

Story: Sometimes the quickest fix is to wind the clock back a few commits.

git reset <commit> moves the branch you're on to another commit. Usually that's back in time: git reset HEAD~1 moves it back one commit, and git reset HEAD~2 two.

The commits your branch leaves behind aren't deleted. They turn faded in the Live Graph, because no branch points at them any more.

Three kinds of reset

Reset always moves the branch. The kind you pick decides what else it changes:

Command Branch Index Working tree
git reset --soft moves unchanged unchanged
git reset --mixed (the default) moves reset unchanged
git reset --hard moves reset tracked files reset
  • --soft only moves the branch. The changes from the commits you left behind are still in the index, staged and ready to commit again. Use it to redo the last commit.
  • --mixed also resets the index. The changes stay in your files but are no longer staged, and files those commits added become untracked. It's what you get with no flag. Use it to commit the same changes differently, such as in two smaller commits.
  • --hard resets your tracked files too. Each one goes back to its version in the commit, and uncommitted changes to it are lost. A tracked file that the commit doesn't have is deleted. Untracked files stay as they are, unless the commit has a file with the same name: then the commit's version replaces yours.

--hard is the dangerous one

A hard reset overwrites your files without asking. The reflog only remembers commits, so it can't bring back an uncommitted change that a hard reset overwrites. Before you run it, check git status for anything you want to keep.

Watch out for a new file you've staged but never committed. Staging made it a tracked file, and the commit you reset to doesn't have it, so a hard reset deletes it from your disk. If you want to keep it, reset first and stage it afterwards.

Committed work is safer. If you reset too far, main@{1} names where main was before the reset, so git reset --hard main@{1} puts it back. Rescuing lost work covers this in detail.

Reset is for your own commits

Like amend, reset rewrites your branch's history. Only reset commits you haven't pushed yet. To undo a commit that others already have, use git revert, from Undoing in public.

New commands

  • git reset
  • git reset --soft
  • git reset --mixed
  • git reset --hard

Explainer: Reset moves a branch

project: your repo, you are here5754f3c Write chapter 15754f3c47cbc80 Write chapter 247cbc80351941a Write chapter 3351941aHEAD → main
The repository at the end of the Explainer.
Graph as text

project: your repo, you are here

  • HEAD → main at 351941a "Write chapter 3"

Branches and tags

  • main at 351941a "Write chapter 3"

Commits, newest first

  1. 351941a "Write chapter 3"; parent 47cbc80; refs: HEAD → main
  2. 47cbc80 "Write chapter 2"; parent 5754f3c
  3. 5754f3c "Write chapter 1"; no parent

Challenge 1 of 3

Story: Chapter 3 went into the record too soon. Take it back out, but don't lose it.

You committed chapter 3 too early. Undo that last commit, so main points at Write chapter 2, but keep chapter 3's change staged, ready to commit again later.

Unlocks after Branching & Merging.

Terminal

Unlocks after Branching & Merging.

This Lesson can be played once Branching & Merging is complete. Its text and Explainer are open to everyone.