How to search for lost files or changes using Git Log

How to search for lost files or changes using Git Log

Table of Contents

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.

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.

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.

Review the commits

This is good enough for me. I can also use the git show command to see the changes in the commit, but this isn’t for everyone. Instead, I can open github and review the changes there by searching for each commit hash and evaluating what happened. You can construct the URL of the commit by appending the commit hash to the end of this URL:

https://github.com/{user-name}/{repository}/commit/{commit-hash}

I can see that the commit I am looking for is e508a9b simply by the commit message, (removed drafts to obsidian). Past Justin thought it was a good idea to move all the drafts to my note taking app, Obsidian, but current Justin has totally forgotten about that.

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



Recent Posts