6

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

Community
  • 1
  • 1
Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
  • Have you tried `git log --reverse --merges --topo-order ..`? The first entry should correspond to commit `h`. – jub0bs Feb 27 '15 at 10:34
  • 1
    you could use this question http://stackoverflow.com/q/8475448/2082964 : how to find the merge following a certain commit – Chris Maes Feb 27 '15 at 10:42

1 Answers1

3

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

Community
  • 1
  • 1
rajeshnair
  • 1,587
  • 16
  • 32