Undoing Safely

The three undos, in increasing order of danger.

git restore --staged file unstages a file, leaving your edits alone. Completely safe.

git restore file throws away uncommitted changes to that file. Destructive, and there is no undo, because those changes were never recorded.

git revert creates a new commit that reverses an old one, leaving history intact. Safe, and the right tool when a change is already shared.

✦ Anything committed can be recovered. Anything not committed cannot.

This is the single most useful thing to understand about git. Once you commit, that state exists forever and almost any mistake is recoverable. Before you commit, your work exists only on disk and a careless restore erases it permanently. That asymmetry is a good argument for committing often, even for work in progress.

SANKOFA SANDBOX
learner:~$

Try it → Init, add, and commit the portfolio. Then stage a change and use `git restore --staged` to take it back out. Watch the file move from Changes to be committed back to Changes not staged.

◈ Graded Task

Build and read a history. In the portfolio, initialize a repository and make two separate commits: first the stylesheet with a message about styling, then the HTML pages with a message about pages. Then change index.html and inspect the difference without committing it.

Checked against the filesystem you leave behind, not the commands you type, so any route that gets the job done counts. 4 visible and 2 hidden checks.

SANKOFA SANDBOX
learner:~$

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Practice Test →
← Back