Hone

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 --short
Two modified files. Only one of them is the mistake.
git restore f.txt
That one, back to the last commit.
git status --short
g.txt is still yours. This is what naming the file buys.
cat f.txt
good -- the committed version.

Your turn

Put f.txt back to how the last commit had it.

git  f.txt

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.