
How to search for lost files or changes using Git Log
- 2024-04-27
- 5 minutes to read
Table of Contents
A long time ago in a galaxy far, far away…
This post is over 12 months old, that's a long time in tech! Please be mindful of that when reading this post, young Padawan, as it could be outdated. I try to keep things up to date as much as possible. If you think something needs updating, please let me know in the comments.
Introduction
I have lost a file in my blog at some point. I have been flipping between themes and somewhere along the way I have somehow lost a handful of draft posts. I wanted to search the history of my Git repository (called the git log) to see if I can find the file.
Depending on what you can remember out the file, or code you are looking for there might be a phrase or word that you can use to search for the file. In my case I am going to search for the string ‘unplugged’. I know the file I want had that as a tag. I am going to use a single word to broaden the search, I can always narrow it down later.
Updates
- 2025-06-29: Added a section on how to find a deleted file
Git Grep
First simple search is to use git grep. This will search the working directory for the string ‘unplugged’. I am pretty sure the file isn’t in the current state of my repository, but it’s worth a try.
> git grep 'unplugged'
content/about/_index.md:Josh Duffneys [blog post](https://duffney.io/the-digital-declutter/) popped up at the right time. Lockdown left me in one long, always-on, plugged-in day from the moment I get up until I eventually drag myself to bed...and it was exhausting. Reading digital declutter led to a few trials of different approaches and I am still learning to remain as [unplugged](../categories/unplugged) as I can.
content/blog/2020/so-long-facebook.md:I have been preparing to [unplug](../../unplugging) from the matrix. I officially unplugged this week. In hindsight, its been a bit of a mad week to kick this off since it was [SQL Bits](https://sqlbits.com) week and being part of the helper team, I have spent a huge amount of time online at an online conference! I am a huge fan of the conference and actively contribute to the social side of it nevertheless, I have made conscious changes to how I have used my phone and it has certainly made a difference.
This search returned two files, neither of which are the ones I am looking for. I am going to need to search the git log.
Git search (for text)
The git log is an ongoing record of changes to the repository. I can search the git log for the string ‘unplugged’ using the following command:
git log -S unplugged --oneline
e508a9b (HEAD -> main, origin/main, origin/HEAD) removed drafts to obsidian
bdcba8e Theme/reader (\#89)
c228595 Squashed commit of the following: (\#70)
944ec6c ignore public folder, cleared out public folder
f5f805c public rebuild
5b821f1 New posts
b6c2992 Added disqus, adjustments to blog posts
415508d Added unplugged post, cleaned up naming, adjusted about to summarise unplugged
69c5c8b Layout of unplugged with images
3f45ffa Turned pages into bundles
91118ad Changes to main pages
f056a35 Adding unplugged menu
The -S flag is used to search for changes that introduce or remove the string ‘unplugged’. The –oneline flag is used to display the output in a single line per commit. Note that the first commit is the most recent commit.
Git search (for path)
So the previous example was to search for a specific string but I had a different use case recently - I noticed some images were broken because the image didn’t exist and I know that I cleared out a section of images I thought weren’t being used and now I need them back.
This time round I’m looking for some specific files in a specific directory. I can use the --diff-filter switch to find the commits that deleted the files.
git log --diff-filter=D -- static/tweets/1246097776204800000.webp`
# if you don't know the specific filename you can just use a path git log --diff-filter=D -- static/tweets/`
So there are two commits returned in the output, and because I use clear commit messages, I can tell that the commit I want is the first one f3eb20474e03b813a6084ee730081167899b76e9.
# git log --diff-filter=D -- static/tweets/
commit f3eb20474e03b813a6084ee730081167899b76e9 (HEAD -> main, origin/main, origin/HEAD)
Author: Justin Bird <>
Date: Fri Apr 11 19:18:01 2025 +0100
removed tweet images
commit c7a0fa0831b0b010e1929bb6e018ef06af14bd33
Author: Justin Bird <>
Date: Wed May 15 10:23:32 2024 +0100
tweet images changed from png to webp
Review the Commit
This is good enough for me. I can also use the git show command to see the changes in the commit, but for most this isn’t going to be the most helpful approach. Instead, I can open github and review the changes there. You can construct the URL of the commit by appending the commit hash to the end of this URL:
https://github.com/justinjbird/site-blog/commit/f3eb20474e03b813a6084ee730081167899b76e9
Git Revert
Alternatively, I could run git revert f3eb20474e03b813a6084ee730081167899b76e9 to reverse the action of that commit. Git will ask you to provide a commit message for the reverted commit and a commit will be created reversing the action of the original commit. The implications of doing that really depend on how long ago the commit happened, how things have changed over time and what that commit covers. We aren’t going to cover that here.
For me, the commit I reverted is from a very long time ago and has a mixture of things in it, so I reverted the commit, grabbed the files I wanted, placed them somewhere else just discarded the reverted commit.
Wrapping Up
I have found the file I was looking for by searching the git log. I used the git grep command to check the working directory and the git log command to search the git log. I was able determine what happened to the file by reviewing the commits.
References
#mtfbwy
Comments