Restore deleted/lost files with git

balastrong

Leonardo Montini

Posted on February 6, 2024

Restore deleted/lost files with git

Remember that file you deleted last week? Too bad, now you need it. Let's see how git restore can help you recover it.

There are a few ways to restore deleted files in git, depending on how far away in the history the file was deleted. I'll cover the most common scenarios and how you can find lost files with git log for those cases where you don't exactly remember when it happened.

As usual, I recorded a video with a demo of how the command works, but if you're just in for the commands you can find everything below.

You just deleted it

The easiest scenario, most IDEs will likely have a button somewhere (vscode has the SCM tab) to undo the deletion.

If you feel faster in the terminal, there's a shortcut with git checkout:

git checkout HEAD path/to/file.ts
Enter fullscreen mode Exit fullscreen mode

Nice and clean, your file is back. Nothing happened.

You already committed the deletion

Not too bad, you can use the exact same command as before, with HEAD~1 to go back one commit:

git checkout HEAD~1 path/to/file.ts
Enter fullscreen mode Exit fullscreen mode

Time travel with git restore

If the situation is more complex, it's probably time to explore how git restore can help you. This command is specifically designed for this kind of operation (while checkout is more general) and comes with a few extra options.

I'd recommend having a look at the official documentation to understand all the options, but here are a few common scenarios, starting with the basic one:

git restore --source=<commit> --worktree -- path/to/file.ts
Enter fullscreen mode Exit fullscreen mode

The deleted file is still on main

You deleted the file in your branch, a few commits ago, but you know for sure it's still in main. Let's make it easy, you can use git restore to bring it back:

git restore --source=origin/main --worktree -- path/to/file.ts
Enter fullscreen mode Exit fullscreen mode

Here I also added origin/ to make sure it picks the file from the remote, but if you're at the latest version on your local main, then --source=main will do its job.

The file was deleted a long time ago, and you don't know when

This is where git log comes in handy. You can use it to find the commit where the file was deleted, and then use git restore to bring it back.

git log --diff-filter=D --summary
Enter fullscreen mode Exit fullscreen mode

This command will show you all the commits where files were deleted, and you can use the commit hash to restore the file.

However, if this happened looong time ago there might be way too many commits to look through. Fear not, you can add a filter with the path to the file:

git log --diff-filter=D --summary -- path/to/file.ts
Enter fullscreen mode Exit fullscreen mode

This will only show the commits where that specific file was deleted. You can now grab the commit hash and use it with git restore:

git restore --source=<commit>~1 --worktree -- path/to/file.ts
Enter fullscreen mode Exit fullscreen mode

Don't forget to add ~1 to the commit hash. The reason is as follow, that hash is the commit where the file has been deleted, which means it's no longer there. With ~1 you're simply going back one commit, the one before the deletion, where the file is still there.

Undo a commit

In some other cases you might simply want to undo a commit. I got you covered! Check out this video where I explore some of the possibilities!

To be fair I undo commits quite often to the point I made a git alias for it that I called git back (it runs git reset HEAD~1 --soft).

I hope you found this article useful and now let me ask you, how do you usually go recover deleted files in git? Let me know in the comments below, I'd love to hear about your workflow!


Thanks for reading this article, I hope you found it interesting!

I recently launched a GitHub Community! We create Open Source projects with the goal of learning Web Development together!

Join us: https://github.com/DevLeonardoCommunity

Do you like my content? You might consider subscribing to my YouTube channel! It means a lot to me ❤️
You can find it here:
YouTube

Feel free to follow me to get notified when new articles are out ;)

💖 💪 🙅 🚩
balastrong
Leonardo Montini

Posted on February 6, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related