shellstreak

Discard local changes to a file

Destructive: deletes work

Short answer

Run git restore <file>. The file goes back to its last committed (or staged) version.

Your uncommitted edits to that file are gone for good: they were never committed, so Git can't bring them back. Check them with git diff <file> first.

The commands, one by one

  1. 1git diff bread.txt

    Look at what you're about to throw away (lines starting with + are yours).

  2. 2git restore bread.txt

    Replaces the file with its last committed version. Other files aren't touched.

For your own repository: Use your own file name.

Try it safely

You scribbled test lines into bread.txt and want the committed recipe back.

learner@shellstreak: ~
Starting your practice shell…

Type your own commands too. Reset puts the repository back to just after the mistake.

Practise this in 5 minutes

Lesson 28, Undo changes, walks you through this undo and the ones around it in a safe terminal, step by step, and checks your answers.

Keep the terminal cheat sheet handy: free to print

Get the shellstreak newsletter

Short terminal and Git tips you can try in a minute, plus a heads-up when new free tools and lessons ship. It lands now and then, never a flood.

No spam, unsubscribe in one click.