Mohsen Laali
Mohsen Laali

Reputation: 483

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.

How could I find this?

I want to check this for many Git commits; 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.

Upvotes: 1

Views: 155

Answers (2)

Schwern
Schwern

Reputation: 165536

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. 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. 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.

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. 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. 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. Instead, do a git blame on the previous commit. 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. Since sometimes changes can be trivial, do a git log -p 4afdb783 to verify it's a significant change. If not, do the same thing again. Run git blame on the commit before that one: git blame -w 4afdb783^. Continue until you get to a significant change.

Upvotes: 4

RaviTezu
RaviTezu

Reputation: 3125

You can make use of 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. 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.

Upvotes: 1

Related Questions