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.txtAn edit you regret.
git restore a.txtThe working-tree change is gone, back to the last committed or staged version. No undo.
echo keep >> a.txt && git add a.txtStaged.
git restore --staged a.txtUnstaged, but the edit stays in the file.
git status --shortM a.txt: modified, not staged.
Your turn
Unstage the file without losing the edit.
git restore a.txt
Try it in a real repository
The trap
git restore file with no flag discards uncommitted edits permanently. Stash or commit first if there is any doubt.