Git:在整个 git 历史记录中显示指定文件中一行的所有各种更改

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

Git: Show all of the various changes to a single line in a specified file over the entire git history

git

提问by findchris

I've looked around, and am not sure if this is possible, but here goes:

我环顾四周,不确定这是否可能,但这里有:

I have a (javascript) file (say /lib/client.js) in which I have a unique identifier assigned to a variable, like so: var identifier = "SOME_IDENTIFIER";

我有一个(javascript)文件(比如/lib/client.js),其中我有一个分配给变量的唯一标识符,如下所示: var identifier = "SOME_IDENTIFIER";

You can think of the identifier like a version number: Periodically, we'll change this variable to a new identifier.

您可以将标识符视为版本号:我们会定期将此变量更改为新的标识符。

What I'd like to do is find all of the unique identifiers we've ever used. How can I do this with git?

我想做的是找到我们曾经使用过的所有唯一标识符。我怎样才能用 git 做到这一点?

I imagine there might be a way to search through the git history, and print the line matching "var identifier =". I could de-dupe this list manually.

我想可能有一种方法可以搜索 git 历史记录,并打印匹配"var identifier =". 我可以手动删除这个列表。

Anyway, I'd appreciate any insight here. Thanks.

无论如何,我很感激这里的任何见解。谢谢。

回答by Alexander Bird

Since git 1.8.4, there is a more direct way to answer your question.

从 git 1.8.4 开始,有一种更直接的方法可以回答您的问题。

Assuming that line 110is the line saying var identifier = "SOME_IDENTIFIER";, then do this:

假设该行110是说var identifier = "SOME_IDENTIFIER";,然后执行以下操作:

git log -L110,110:/lib/client.js

This will return every commit which touched that line of code.

这将返回触及该代码行的每个提交。

[Git Documentation(see the "-L" command line paramenter)]

[ Git 文档(请参阅“-L”命令行参数)]

回答by rob

See the man page for git-logand gitdiffcore. I believe this command would do it, but it might not be quite right:

请参阅手册页git-loggitdiffcore。我相信这个命令可以做到,但可能不太正确:

git log -G "var identifier =" file.js


EDIT:Here's a rough start for a bash script to show the actual lines. This might be more what you're looking for.

编辑:这是显示实际行的 bash 脚本的粗略开始。这可能是您正在寻找的更多内容。

for c in $(git log -G "something" --format=%H -- file.js); do
    git --no-pager grep -e "something" $c -- file.js
done

It uses git log -Gto find the interesting commits, using --format=%Hto produce a list of commit hashes. It then iterates over each interesting commit, asking git grepto show the lines from that commit and file that contain the regex, prefaced with the commit hash.

它用于git log -G查找有趣的提交,--format=%H用于生成提交哈希列表。然后它遍历每个有趣的提交,要求git grep显示包含正则表达式的提交和文件中的行,并以提交哈希开头。



EDIT:Changed to use -Ginstead of -Sas suggested in comments.

编辑:更改为使用-G而不是-S如评论中建议的那样。

回答by Ethan Brown

You can also do this with gitk:

你也可以用 gitk 做到这一点:

gitk file.js

In the "commit" drop down, choose "adding/removing string:" and in the text box next to it, enter "var identifier =", and any commits that add or remove lines that contain that string will be highlighted.

在“提交”下拉列表中,选择“添加/删除字符串:”并在其旁边的文本框中输入“var identifier =",任何添加或删除包含该字符串的行的提交都将突出显示。

回答by Andrew

If you adapt @rob's answer just a bit, git logwill basically do this for you, if all you need is a visual comparison:

如果您稍微调整@rob 的答案,git log基本上会为您做这件事,如果您只需要一个视觉比较:

git log -U0 -S "var identifier =" path/to/file

-U0means output in patch mode (-p), and show zero lines of context around the patch.

-U0表示以补丁模式 ( -p)输出,并在补丁周围显示零行上下文。

You can even do this across branches:

您甚至可以跨分支执行此操作:

git log -U0 -S "var identifier =" branchname1 branchname2 -- path/to/file

There may be a way to suppress the diff header, but I don't know of one.

可能有一种方法可以抑制 diff 标头,但我不知道一种方法。

回答by slumos

In magit, you can do this with

magit 中,您可以使用

l, =L

It will then ask you for file and start,end lines.

然后它会询问您文件和开始,结束行。