Lessons · Git · putting one back, not the lot
One file back to how it was
git restore takes one named file back. Every other file in the tree is left exactly as it is.
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
The blunt instrument is reset --hard, and it takes the whole tree with it. Naming the file is the difference between undoing a mistake and undoing an afternoon.
How to think about it
Name the file. If you are not certain you want the edit gone, stash instead -- a stash is recoverable and a restore is not.
Worked example
git status --shortTwo modified files. Only one of them is the mistake.
git restore f.txtThat one, back to the last commit.
git status --shortg.txt is still yours. This is what naming the file buys.
cat f.txtgood -- the committed version.
Your turn
Put f.txt back to how the last commit had it.
git f.txt
Try it in a real repository
The trap
There is no undo for this. The edit was never committed or stashed, so nothing in the repository has a copy of it.
Practise putting one back, not the lot on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.