Hone

Lessons · Git · putting a file back

Taking a change back

git restore file discards unstaged edits to that file. git restore --staged file unstages it and keeps your edits. The first has no undo.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

You will stage the wrong file, and you will make an edit you want gone. Both are one command, and knowing which flag is which is the difference between tidy and lost work.

How to think about it

Unstage: --staged, and the file is untouched. Discard: no flag, and the edit is gone for good. When unsure, stash instead; a stash can be brought back.

Worked example

echo mistake >> a.txt
An edit you regret.
git restore a.txt
The working-tree change is gone, back to the last committed or staged version. No undo.
echo keep >> a.txt && git add a.txt
Staged.
git restore --staged a.txt
Unstaged, but the edit stays in the file.
git status --short
M a.txt: modified, not staged.

Your turn

Unstage the file without losing the edit.

git restore  a.txt

The trap

git restore file with no flag discards uncommitted edits permanently. Stash or commit first if there is any doubt.

Practise putting a file back on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.