DEV Community

miku86
miku86

Posted on • Updated on

Git: Find specific, deleted content in a file

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

Top comments (0)