A good[ish] website

Web development blog, loads of UI and JavaScript topics

Git: deleted a bunch of files accidentally, how to get them back?

Filed under: Webdev— Tagged with: git

You know the the sinking feeling when you accidentally wiped out half of the repo with a harmless looking rm -rf. No worried though, there’s a high change of getting it all back if it’s a Git repo.

Deleted – nothing else after that

Just deletion, and no staging or nothing after that. Checkout should do the trick:

$ git checkout .

Deleted – added to the staging area

File got deleted and then added to the staging area, at this point you noticed that something’s wrong:

# Reset the head first
$ git reset HEAD <file>

# Then checkout
$ git checkout <file>

# Or if it’s a ton of files, then use the dot
$ git reset HEAD .
$ git checkout .

Deleted — added, and committed

If you went the whole way and committed all the deletions, then rollback with reset to the last commit. The following will roll back 1 commit:

$ git reset --hard HEAD~1

Or rollback to s specific commit ID:

$ git reset --hard 80789fa6a027b3ee3dec760e5784c61b7e4c9d17

Get the commit ID with git log.


It’s relatively hard to delete anything permanently in Git.

Also, see this nice thread on the dangers of rm and how to safeguard yourself from yourself.

Comments would go here, but the commenting system isn’t ready yet, sorry. Tweet me @hiljaa if you want to make a correction etc.

  • © 2021 Antti Hiljá
  • About
  • Follow me in Twatter → @hiljaa
  • All rights reserved yadda yadda.
  • I can put just about anything here, no one reads the footer anyways.
  • I love u!