Elena' s AI Blog

Restoring deleted files in Git

05 Dec 2023 / 3 minutes to read

Elena Daehnhardt


Matrix fell, Midjourney Nov 2023


Introduction

Recently, I was working late; it was terrible weather outside, and something happened with my Wi-Fi connection. I had a glitch with my repository. I am not sure whether it was a coincidence with my late work or the weather :)

I had a glitch, quite a bad one, and many images were deleted from my repository. It is such a waste of time. I am fixing it now. See how I do it here so that you can restore your files when the bad weather and Glitch come :)

Restoring the deleted files

Do not worry. Everything will be fine! We will get the deleted files back!

First, we must find out the exact commit when the files were deleted. We use the “D” filter with git log:

git log --diff-filter=D --summary

Now I see it happened yesterday when synchronising my repository from another computer.

commit 45a2d299ef3d....
Author: Elena Daehnhardt <email@gmail.com>
Date:   Wed Nov 29 12:05:06 2023 +0100

    create_references

 delete mode 100644 images/ai_art/dalle/elena/dall.e.22.18.39.png
 delete mode 100644 images/ai_art/dalle/elena/dall.e.22.18.45.png
 delete mode 100644 images/ai_art/dalle/elena/dall.e.22.18.50.png

See the commit hash (45a2d299ef3d….)? Cope past your COMMIT_ID. We will use it next.

Restore the deleted files

At this commit hash ID, we deleted files to be restored. These files are still stored in the previous commit. Thanks, Git!

For referring to the previous commit, we use tilde ~1 with the number denoting the 1st grandparent of the commit we define with it:

To place that file back, we use:

git checkout COMMIT_ID~1 images/ai_art/dalle/elena/dall.e.22.18.39.png

We get:

Updated 1 path from ae8c184

Yes, we have that file in our working tree!

Ok, but more files were deleted in that folder. Can we restore them all? Sure, let’s use the folder name:

git checkout COMMIT_ID~1 images/ai_art/dalle/elena/
Updated 3 paths from ae8c184

To summarise, use:

# Find the guilty commit
git log --diff-filter=D --summary

# Restore from the previous
git checkout COMIIT_ID~1 path/to/deleted_file

# Commit it back to your master branch

See what files changed at the commit

Sometimes, seeing what files changed at the COMMIT_ID is pretty helpful.

git show --name-only COMMIT_ID
images/ai_art/midjourney/computer/laptop_fantastic.jpg

To see a summary of what happened:

git show --name-status COMMIT_ID
    laptop_fantastic.jpg => 600px

D       images/ai_art/computer/laptop_fantastic.jpg
A       images/ai_art/midjourney/computer/laptop_fantastic.jpg

Conclusion

In this post, we have restored deleted files, which is easy when you know how to :)

References

Using Git

Did you like this post? Please let me know if you have any comments or suggestions.

Git posts that might be interesting for you




desktop bg dark

About Elena

Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.




Citation
Elena Daehnhardt. (2023) 'Restoring deleted files in Git', daehnhardt.com, 05 December 2023. Available at: https://daehnhardt.com/blog/2023/12/05/git-restoring-deleted-files/
All Posts