shellstreak

Restore a deleted file in Git

Safe: nothing is lost

Short answer

If the deletion isn't committed, run git restore <file>: Git puts back the last committed version.

The commands, one by one

  1. 1git status -s

    The file shows D: deleted on disk, still in Git.

  2. 2git restore scones.txt

    Writes the last committed version of the file back to disk.

For your own repository: If you also staged the deletion (D in the first column), run git restore --staged scones.txt first.

Try it safely

You ran rm scones.txt by mistake.

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.