142

I want to find out in which commit did I add the code given below:

if (getListView().getChildCount() == 0)
                getActivity().findViewById(android.R.id.empty).setVisibility(View.VISIBLE);

How do I achieve this?

Flip
  • 6,233
  • 7
  • 46
  • 75
Harshal Kshatriya
  • 5,630
  • 11
  • 44
  • 60

5 Answers5

132
git log -S searchTerm

gives you the commits in which the search term was introduced.

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
Rahil Ahmad
  • 3,056
  • 1
  • 16
  • 21
  • 10
    I think that's the best answer as the OP wanted to search for some code (not file) added. – dr_ Mar 14 '18 at 08:59
  • 1
    I agree - I had a problem where all the repository went through a "re-indent code" session, to shrink tabs from 4 spaces to 2. From that moment on, all git blame will always reveal the guy who re-indented the code - not the one who introduced it. git log -S does the trick. – Motti Shneor May 24 '18 at 05:07
  • Just to mention, searchTerm should be in string quotes. Something like `git log -S "createUserFunction()"` – KE Keronei Feb 14 '22 at 12:41
  • I use this command and I got different results when executed against different main branches (for example UAT vs PROD). Why is that? – tarekahf Nov 12 '22 at 22:35
97

Run git blame on the file. It'll show you the commit ID, the date and time, and who committed it- for each line. Then just copy out the commit identifier and you can use it in git log <commit> or git show <commit>.

For example, I've got a file, called test.txt, with lines added on different commits:

$ cat test.txt
First line.
Second line.

Running the git blame:

$ git blame test.txt
^410c3dd (Leigh 2013-11-09 12:00:00 1) First line.
2365eb7d (Leigh 2013-11-09 12:00:10 2) Second line.

The first bit is the commit ID, then name, then date, time, time zone, and finally the line number and line contents.

Leigh
  • 12,038
  • 4
  • 28
  • 36
  • 27
    The problem is that this only shows when those files where last changed and not when they were added. – ensonic Oct 29 '15 at 18:09
  • @ensonic This answer may be interesting if that's the case (e.g. the line was moved, or a whitespace change): http://stackoverflow.com/a/5816177/812680 – mcls Jan 12 '17 at 09:26
  • 3
    Useful additional is to grep the results `git blame test.txt | grep 'First line'` – Jonathan Lockley Aug 17 '17 at 10:51
  • 4
    That does not answer the OP's question. We had a problem where all our repository went through a "re-indent code" change, to shrink tabs from 4 spaces to 2. From that moment on, all git blame will always reveal the guy who re-indented the code - not the one who introduced it. git log -S does the trick. – Motti Shneor May 24 '18 at 05:08
54

There is something quicker than issuing a blame on the full file. If the line is ${lineno} and the file is ${filename} you can:

git blame -L ${lineno},${lineno} ${filename}

Example:

git blame -L 2,2 test.txt
Matt Parker
  • 26,709
  • 7
  • 54
  • 72
cforbish
  • 8,567
  • 3
  • 28
  • 32
  • 6
    This doesn't actually answer the question - unless that line of code was never changed. It will reveal who is the user who last changed these lines of code. Not the one who introduced them. git log -S, though, will. However, useful technique... – Motti Shneor May 24 '18 at 05:05
  • With out a doubt, the right solution! – Frank Thomas Nov 02 '21 at 15:56
40
git log -S "mention here line of code" [file-path]    

For example:

git log -S "First line" test.txt         

Providing the file name with its path is obvious because, most of the time, we want to know who introduced a particular code segment in a particular file.

Daniel Holmes
  • 1,952
  • 2
  • 17
  • 28
Anshul Singhal
  • 1,983
  • 20
  • 25
  • Excuse me, I can't get it - what should be put in the "mention here line of code". the Line-number? the actual line contents? a Text to search for? Can you please be clearer ? – Motti Shneor May 21 '18 at 09:30
  • 4
    OK. I used it, and I marvelled at the results. this A LIFE SAVER. really. In our rather clumsy and lousy corporate repository, some wise-guy decided to change the tab spacing fro 4 to 2 over 1.2M lines of code. Of course from that moment on - git blame is meaningless - because every damn line has changed by that same nasty commit of re-indenting the sources. So - this really gave me the rope i needed - to find who "invented" a line of code, and not the last person to have changed it. THANK YOU. Big hand. – Motti Shneor May 22 '18 at 07:07
  • Thanks for appreciating. – Anshul Singhal May 22 '18 at 16:28
  • 3
    In the Linux terminal it required single quotes for the line of code: `git log -S 'some code' path/to/file.c` – Daniel Holmes May 17 '19 at 09:33
  • This was a lifesaver! Thank you! – Saif Al Falah Jun 17 '19 at 08:49
3

If code is being maintained in Git repository and intellij IDE along with Git plugin is being used to browse the code then i have found the following way very intuitive:

  1. Open the file in intellij
  2. Go to context menu -> Git -> Annotate.
  3. New window will appear where each line in new window tells who made that line commit.

Following screenshots are for the references:

enter image description here

Anshul Singhal
  • 1,983
  • 20
  • 25
  • Very useful. I don't get a new window, but I do get dates and contributor names next to the line numbers in the usual file editor pane, which if clicked on show the specific commit. Thanks Anshul! – Nolan Strait Apr 04 '22 at 22:35