Level 13

Reset

Repoint a branch marker to an earlier commit, and see what Git does with the changes that would be lost.

Welcome to Level 13, where you learn yet ANOTHER way to undo changes. Like git revert, it works on commits, but the way it works is totally different.

Instead of making a new page that applies the opposite change... what if you just moved your branch marker to a different page?

Step 1 of 6
0 xp

Move the marker

In scrapbook terms, that is removing a section marker from a page and sticking it onto a different one. In Git it is called resetting the branch, and git reset is the command.

git reset <commit>

Say you want to undo the revert page from Level 12. It is at the front of dev. If you moved the dev marker and HEAD back to the previous page, the revert page would no longer be reachable by tracing arrows back from any marker. It would effectively be gone from the dev branch.

The caret

You want to move dev back one commit before the current one. Git has a shortcut for that: the caret, ^. HEAD^ means the page just before the one HEAD sits on.

(HEAD^ and HEAD~1 mean the same thing. The tilde counts generations back; the caret picks a parent. On a page with one parent, they agree.)

Reset dev back one page

Reset the active branch back one page:

Type this into the terminal below
git reset HEAD^

Look. The dev marker and HEAD now sit one commit back from where they were, and the revert commit no longer shows up in the log. No marker points at it, and it cannot be reached by tracing arrows back from dev or main.

How did Git know which branch to reset? Simple: it resets the branch HEAD points to, the active branch. dev was active, so dev got reset to HEAD^.

caution Moves dev from 9bf8a3f to aed7805 (mixed reset).

What the?!

Now look at the wall. Why is brew.py showing up as a modified file?

By default, Git tries to prevent you from losing work. How nice. Any code change that would otherwise be lost in a reset gets put back into the working directory, so you get a second chance to decide: keep the changes and add to them, stash them for later, or get rid of them. The revert's Sip coffee edit is sitting on your table as an uncommitted change. The revert page itself is drawn faded: still in the binder for a while, but no marker can reach it.

Your turn: clear the table

You do not want that leftover change. Which command gets rid of it?

Soft, mixed, hard

git reset has three moods. The one you just used, mixed, is the default: it moves the marker and leaves the changes on your table. --soft moves the marker and leaves the changes staged, on the mat. --hard moves the marker and throws the changes away, table included.

The first two are gentle. The third is the one to run git status before, because whatever is on the table goes with it.

git-sim
Initial CommitDevlands

The graph appears here when the first command runs. The simulations are drawn by git-sim from a real repository.

The scrapbook · one picture for every Git idea

The scrapbook picture appears as the story is told.