Level 11

Restore

Move a staged change back to the working directory, and discard changes you no longer want.

OMG, Level 11. Did I mention this level will be short, sweet and RESTORE-ative? Wait, did I make that joke already? Whatever. You try coming up with sixteen levels of Git puns.

When you learned git add and git commit back in Level 3, your mementos moved in one direction: FORWARDS. From the tabletop, to the staging area, to a new page in the book.

Step 1 of 6
0 xp

Backwards

Sooner or later you will run into situations where it helps to move file changes BACKWARDS: from the staging area back to the working directory, or off the table entirely.

It is like realizing you do not want a memento on your next page after all, so you move it back to the tabletop, or take it off the table completely. Moving changes backwards is done with git restore. Think of it as the opposite of git add.

First, forwards

To demonstrate, start by moving the modified brew.py onto the mat with git add, as if it were going onto the next page.

Type this into the terminal below
git add brew.py
safe 'add' stages files; nothing is discarded.

Two flavours of restore

git restore --staged <filename>

The --staged flag tells Git to move the file from the staging area back into the working directory. The change is still on your table; it is just not on the mat any more.

git restore <filename>

Without --staged, restore throws away the modifications to a file and puts it back to how it was on its last page. That one is permanent: an edit that was never committed or stashed has no copy anywhere.

Your turn: back to the table

Pretend you made a mistake and are not actually ready to commit the change in brew.py. You want to unstage it without losing the edit. Which command?

Off the table entirely

Now pretend the change was completely unnecessary, because you are all out of coffee. Discarding the edit means brew.py goes back to Chug coffee, the code as of the latest commit. The memento stays; only the change from Chug coffee to Pour some more is undone.

The pre-flight check under the graph will call this destructive. It is right: once discarded, the change cannot be brought back.

Discard the change

Discard the change:

Type this into the terminal below
git restore brew.py

Yahoo. Your code changes in brew.py are all gone. The file itself is not: it just contains the code that existed as of the latest commit. Since there are no modified files left in your working directory, the wall is totally empty.

And speaking of empty, so is my material for Level 11. But do not worry... YOU PASSED.

destructive Overwrites 1 file(s) with the index/HEAD version, discarding local modifications:
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.