Git 责备提交的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13692072/
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-10 15:01:38  来源:igfitidea点击:

Git blame committed line

git

提问by Gustavo Litovsky

Is it possible somehow for GIT to find which commit introduced a specific line of code in a specific file? This is assuming that there have been many commits since that line was added. Or is this something that must be done in a script while looking at the git blame of all the commits for the file in which the line is present?

GIT 是否有可能以某种方式找到哪个提交在特定文件中引入了特定代码行?这是假设自从添加该行以来已经有很多提交。或者这是必须在脚本中完成的事情,同时查看该行所在文件的所有提交的 git blame ?

To clarify

澄清

Original file->Line Added to file and committed -> Many other commits adding other lines and changing the code

原始文件-> 行添加到文件并提交-> 许多其他提交添加其他行并更改代码

采纳答案by phschoen

You can use git blame -l filenameto get the SHA1 hash from when the line was changed.

您可以使用git blame -l filename从更改行时获取 SHA1 哈希值。

You also can use --reverse:

您也可以使用 --reverse:

--reverse

Walk history forward instead of backward. Instead of showing the revision in which a line appeared, this shows the last revision in which a line has existed. This requires a range of revision like START..END where the path to blame exists in START.

- 逆转

向前而不是向后走历史。这不是显示出现行的修订版,而是显示存在行的最后一个修订版。这需要一系列修订,例如 START..END,其中 START 中存在归咎路径。

See: http://www.kernel.org/pub/software/scm/git/docs/git-blame.html

请参阅:http: //www.kernel.org/pub/software/scm/git/docs/git-blame.html

回答by ct.kuo

It seems you can use git log -L to check file. For example, I want to check the commits for file.c, line from 55 to 60:

看来您可以使用 git log -L 来检查文件。例如,我想检查 file.c 的提交,第 55 到 60 行:

git log -L55,60:file.c

回答by user2784627

You can blame against the specific code string like this.

您可以像这样指责特定的代码字符串。

git log -S 'THE-CODE' 

回答by Ryan Bigg

Assuming that the line hasn't changed since it's been added, then simply:

假设该行自添加以来没有改变,那么只需:

git blame <that file>and then look for the line that you're after.

git blame <that file>然后查找您要查找的行。

If the line has been modified since it's been added, you can still git blameto find out the last commit on which the line was altered, and then git checkout <sha>~1(where shais the sha that shows up in the original git blame) and git blameagain to go back in the history for that line.

如果该行自添加以来已被修改,您仍然git blame可以找出该行被更改的最后一次提交,然后git checkout <sha>~1sha原始文件中显示的 sha在哪里git blame)并git blame再次返回历史记录那条线。