git 查找行被删除的时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12591247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:32:06  来源:igfitidea点击:

Find when line was deleted

git

提问by matthewwithanm

Earlier today I discovered that some code was missing from my Git repository. I knew some of the missing text, and the file that it was in, so I used git log -S'missingtext' /path/to/file.

今天早些时候,我发现我的 Git 存储库中缺少一些代码。我知道一些丢失的文本,以及它所在的文件,所以我使用了git log -S'missingtext' /path/to/file.

However, the only thing that came back was the commit in which I added the line containing the missing text. The text wasn't present in HEAD, and the commit that added it was present in my branch, so I knew that one of the commits in my branch's history must have removed it, but it wasn't showing up.

但是,唯一返回的是我在其中添加了包含缺失文本的行的提交。该文本不存在于 HEAD 中,添加它的提交存在于我的分支中,所以我知道我的分支历史记录中的其中一个提交肯定已经删除了它,但它没有出现。

After some manual searching, it turned out that the line was removed accidentally while resolving a conflict for a merge. So I'm wondering:

经过一些手动搜索,结果是在解决合并冲突时意外删除了该行。所以我想知道:

  1. Is this the reason why pickaxe couldn't find the commit that deleted the line?
  2. How could I have found where "missingtext" was deleted without digging through the history manually?
  1. 这就是镐找不到删除该行的提交的原因吗?
  2. 我怎么能在不手动挖掘历史记录的情况下找到“missingtext”被删除的地方?

Any insight on #1 would be great (I assumed that git log -Swould give me my answer), but my real question is #2 since I'd like to be able to avoid this in the future.

对#1 的任何见解都会很棒(我认为这git log -S会给我答案),但我真正的问题是#2,因为我希望将来能够避免这种情况。

回答by lettertwo

git logdoesn't show a diff for merge commits by default. The -cor --ccflags should do the trick:

git log默认情况下不显示合并提交的差异。该-c--cc标志应该做的伎俩:

git log -c -S'missingtext' /path/to/file

git log -c -S'missingtext' /path/to/file

More discussion/explanation is here.

更多讨论/解释在这里

回答by James

There is a great answer to this on Super User: Git: How do I find which commit deleted a line?

超级用户对此有一个很好的答案: Git:我如何找到哪个提交删除了一行?

git blame --reverse START.. file.ext

This will show, for each line, the last commit where the line was present - say hash 0123456789. The next commit to follow will be the one which removed it. Use git logand search for hash 0123456789 and then its successor commit.

对于每一行,这将显示该行所在的最后一次提交 - 比如说哈希 0123456789。接下来的提交将是删除它的提交。使用git log并搜索哈希 0123456789,然后搜索其后续提交。

回答by wolverdude

Quick and dirty way #2 - use a for loop.

快速而肮脏的方式#2 - 使用 for 循环。

for commit in $(git log --pretty='%H'); do
    git diff -U0 --ignore-space-change "$commit^" "$commit" | grep '^-.*missingtext' > /dev/null && echo "$commit"
done

This will include all merge changes because it explicitly specifies the base commit for the diff. I came up with this because git log -c -S...was giving me a bunch of false-positives. Also, when I specified a filepath in the initial git logcommand, it skipped the commit I was looking for.

这将包括所有合并更改,因为它明确指定了差异的基本提交。我想出这个是因为git log -c -S...给了我一堆误报。此外,当我在初始git log命令中指定文件路径时,它跳过了我正在寻找的提交。

Since this may run for a while, you can specify -non the git logcommand or put an && breakat the end of the loop if you only need 1 result.

由于这可能会运行一段时间,如果您只需要 1 个结果,您可以-ngit log命令上指定或&& break在循环末尾放置一个。