Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the merge commit that modified a given line?

We're using a git-flow style workflow, and we want to find out which pull request included changes to a given line of code.

Suppose we have the following history:

       c---e---g
      /         \
-a---b---d---f---h--- master

My pull request was merged in h with commit message "Merge pull request #123".

If I do a git blame on the lines of code added in the pull request, it shows me e, rather than h.

12345678 (Wilfred Hughes           2015-02-02 15:22:40 +0000 402) # Some old code
e        (Wilfred Hughes           2015-02-12 15:22:40 +0000 402) # Added in the PR, line 1
e        (Wilfred Hughes           2015-02-12 15:22:40 +0000 403) # Added in the PR, line 2
56789012 (Wilfred Hughes           2015-02-26 17:24:18 +0000 404) # More old code

How can I find the merge commit for a given line of code in git?

(Note this is different from this related question as I'm starting with lines of code, not commits).

like image 830
Wilfred Hughes Avatar asked Feb 27 '15 10:02

Wilfred Hughes


1 Answers

A merge-commit would only appear in git-blame if you had resolved conflict in that commit.

For normal merges without conflict, h would never appear in git blame as it did not change in any line of code and merely merged g and f.

So what you want can be achieved in 2 phases

  1. First find which commit impacted line of code you are interested in. This can be achieved via git blame -L option ( comes in git 1.8.4). Give a range of 10+ lines as it is likely exact line number might have changed.

    $ git blame -L200,+10 -- filename.ext

  2. Find the first merge commit following the commit found in step 1 So you would have to first find which commit impacted the line of code you are interested in. This can be achieved as answered in Find merge commit which include a specific commit

like image 126
rajeshnair Avatar answered Oct 22 '22 08:10

rajeshnair