git – How to find out in which commit a particular code was added?

Question:

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

How do I achieve this?

Answer:

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:

Running the git blame:

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

Leave a Reply