简体   繁体   English

对于给定的 Git 提交,我如何找出谁(什么用户)提交了已删除的代码行?

[英]How can I find out who (what user) commits deleted lines of code for a given Git commit?

In Git version control, I want to find out who (what user) originally commits deleted lines of code for a given commit.在 Git 版本控制中,我想找出谁(什么用户)最初提交了给定提交的已删除代码行。

How could I find this?我怎么能找到这个?

I want to check this for many Git commits;我想检查这个是否有很多 Git 提交; therefore using a graphical interface is not an option.因此使用图形界面不是一种选择。 I am looking for a Git command (or consecutive commands) to automate this process with a Python script.我正在寻找一个 Git 命令(或连续命令)来使用 Python 脚本自动执行此过程。

If I understand the question correctly, you want to find who originally added a line which was just deleted?如果我理解正确的话,你想找到谁最初添加了刚刚删除的行? For example, in this commit we see the line =head1 C<import> deleted.例如,在此提交中,我们看到行=head1 C<import>已删除。 Who wrote that line originally?那句话最初是谁写的?

--- a/lib/perl5i.pm
+++ b/lib/perl5i.pm
@@ -1118,27 +1118,29 @@ Example:
     from CPAN or another repository.  Your library paths are:
         Indented list of paths, 1 per line...

-=head1 C<import>

-This subroutine is called automatically, see L<perlfunc/import>.
+=head1 Turning off features
...

The command you want is git blame .你想要的命令是git blame This will show which commit last modified each line.这将显示最后修改每行的提交。 For our example, git blame lib/perl5i.pm would show us who last touched each line of the file.对于我们的例子, git blame lib/perl5i.pm会告诉我们谁最后接触了文件的每一行。

b755dda5 (Michael G. Schwern      2009-04-22 21:29:08 -0700    1) package perl5i;
b755dda5 (Michael G. Schwern      2009-04-22 21:29:08 -0700    2) 
8baa7538 (Michael G. Schwern      2010-01-29 21:34:14 -0800    3) ######################################
a6231688 (Michael G. Schwern      2010-03-14 13:55:50 -0700    4) # The real code is in perl5i::2      #
8baa7538 (Michael G. Schwern      2010-01-29 21:34:14 -0800    5) # Please patch that                  #
8baa7538 (Michael G. Schwern      2010-01-29 21:34:14 -0800    6) ######################################
...

That's the commit, the author's name, when the commit happened, and the line.那是提交、作者的名字、提交发生的时间和行。

This isn't necessarily the original author of the line.这不一定是该行的原始作者。 Even a commit that makes a simple whitespace change will show up.即使是进行简单空白更改的提交也会显示出来。 To avoid this, add -w to ignore whitespace changes.为避免这种情况,请添加-w以忽略空白更改。 git blame -w lib/perl5i.pm . git blame -w lib/perl5i.pm

But that's for the current commit.但那是针对当前的提交。 You can ask for a blame as of a particular commit, our example is 4519fb29cef which deleted the line.您可以要求对特定提交负责,我们的示例是4519fb29cef删除了该行。 If we do git blame -w lib/perl5i.pm 4519fb29cef the line will already have been deleted so it won't show up in the blame.如果我们执行git blame -w lib/perl5i.pm 4519fb29cef该行将已经被删除,因此它不会出现在 blame 中。 Instead, do a git blame on the previous commit.相反,对之前的提交做一个git blame git blame -w lib/perl5i.pm 4519fb29cef^ . git blame -w lib/perl5i.pm 4519fb29cef^

...
4afdb783 (Lars Dɪᴇᴄᴋᴏᴡ 迪拉斯     2013-03-01 11:16:27 +0100 1121) =head1 C<import>
4afdb783 (Lars Dɪᴇᴄᴋᴏᴡ 迪拉斯     2013-03-01 11:16:27 +0100 1122) 
4afdb783 (Lars Dɪᴇᴄᴋᴏᴡ 迪拉斯     2013-03-01 11:16:27 +0100 1123) This subroutine is called automatically, see L<perlfunc/import>.
...

Commit 4afdb783 by Lars Dɪᴇᴄᴋᴏᴡ 迪拉斯last touched that line. Lars Dɪᴇᴄᴋᴏᴡ 迪拉斯4afdb783触及了那条线。 Since sometimes changes can be trivial, do a git log -p 4afdb783 to verify it's a significant change.由于有时更改可能微不足道,请执行git log -p 4afdb783以验证它是否是重大更改。 If not, do the same thing again.如果没有,再做同样的事情。 Run git blame on the commit before that one: git blame -w 4afdb783^ .对之前的提交运行git blamegit blame -w 4afdb783^ Continue until you get to a significant change.继续,直到发生重大变化。

You can make use of git blame: https://git-scm.com/docs/git-blame您可以使用 git blame: https ://git-scm.com/docs/git-blame

git blame accepts a file and annotates each line in the given file with information from the revision which last modified the line. git blame接受一个文件,并使用来自最后修改该行的修订版的信息来注释给定文件中的每一行。 Optionally, start annotating from the given revision.或者,从给定的修订开始注释。

Or else, you can make use of a graphical tool like gitk to see, what does a commit changes and all.或者,您可以使用像gitk这样的图形工具来查看提交更改的内容等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM