Level 12

Revert

Undo the changes from an earlier commit by adding a new commit that does the opposite.

In Level 11 you undid working directory modifications and staged changes with git restore. But wait a minute. What if you already committed a change, only to realize later that you want to undo it? git restore only works on files, not on commits, so it will not help you there.

Here in Level 12 you learn git revert, which lets you undo the changes in ANY commit.

Step 1 of 6
0 xp

Find the page

Say you want to undo the change you made to brew.py the last time you updated it, from Sip coffee to Chug coffee. First, identify the page.

Type this into the terminal below
git log

Hover the commits in the log until you find the one whose message is Update brew.py. It is the fourth from the front.

safe 'log' only reads; nothing in the repository changes.

Counting back from HEAD

You could refer to that commit by its ID. But for commits near HEAD there is a handier way: count how far back it is. HEAD~3 means "three commits behind HEAD", and the page that updated brew.py is exactly that.

Naming pages relative to HEAD saves you copying IDs about. You met HEAD~2 in Level 9; this is the same idea.

What revert does

git revert <commit>

Shouldn't undoing a commit just delete it? No, and here is an important point about history in Git. Each commit ties back to its parent, forming an uninterrupted chain. Going back and deleting a page from the middle would leave a hole that breaks your history. You cannot do that.

So git revert does something smart. It does not remove the old commit. It calculates the OPPOSITE of the change in that commit and creates a brand new commit that applies it.

Add the opposite page

Revert the page three behind HEAD:

Type this into the terminal below
git revert HEAD~3

A new page appears at the front of dev. Hover it: the message is Revert "Update brew.py", and its diff changes brew.py back from Chug coffee to Sip coffee. The old page is still exactly where it was, and the chain is intact.

The final effect: you undid the changes in an old commit without messing up the past history of your project.

safe 'revert' creates a new commit undoing another; existing history is untouched.

The revert page

That is the book now. The undo is a page like any other, with its own stamp, and the original page it undoes is still there for anyone to read.

If you want to get really crazy, you can even undo the changes in your undo commit, which would basically be a... REDO? That is a bit silly. Plus there is another way, which you will discover in the next level. Wink, wink.

Your turn: undo the undo

You changed your mind again and want Chug coffee back, without breaking the chain. Which command?

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.