loading...

Git: Find specific, deleted content in a file

miku86 profile image miku86 Updated on ・1 min read

Problem

I worked in a file, made some commits.

Then I wanted to add some old text, that I had deleted some 10 commits ago, back into the file.

Solution

So I only knew, that the path was content/pages/about.md and the text had newsletter in it.

Simple Solution

git log -p -S <string-to-search> -- <path-to-file>

In my case: git log -p -S 'newsletter' -- content/pages/about.md

You will see all changes for this specific file.
You will have to scan manually through the file.

Better solution

git grep <regex-to-search> $(git rev-list --all -- <path-to-file>) -- <path-to-file>

In my case: git grep newsletter $(git rev-list --all -- content/pages/about.md) -- content/pages/about.md

Further Reading

Posted on by:

miku86 profile

miku86

@miku86

Mentor & Educator & Senior Web Developer - I help people to reach their (career) goals. => https://miku86.com/mentoring

Discussion

markdown guide