Git:查找文件中特定的、已删除的内容

2025-06-07

Git:查找文件中特定的、已删除的内容

问题

我在一个文件中工作,做了一些提交。

然后我想将一些 10 次提交前删除的旧文本添加回文件中。

解决方案

所以我只知道,路径在那里,并且其中content/pages/about.md有文本。newsletter

简单的解决方案

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

就我而言:git log -p -S 'newsletter' -- content/pages/about.md

您将看到此特定文件的所有更改。
您需要手动扫描该文件。

更好的解决方案

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

就我而言:git grep newsletter $(git rev-list --all -- content/pages/about.md) -- content/pages/about.md

进一步阅读

文章来源:https://dev.to/miku86/git-find-specific-deleted-content-in-a-file-5h33
PREV
使用 JavaScript 操作 HTML DOM
NEXT
CSS 挑战 #1 | 面包屑导航