threads / discuss / 40307

Re: Seeing git history of file including merge changes

Subject: Re: Seeing git history of file including merge changes

## tl;dr

2 messages between Sep 9, 2015 and Sep 9, 2015.

replies: 1people: 2as markdown or json

Dov Grobgeld· Sep 9, 2015, 08:26 UTC · lore

I asked the following question on StackOverflow a while ago, but did not receive any answers so I'll try here.

We have git repository where a file was changed on a topic branch and this branch was then merged back into the main branch. Due to various reasons the merge was made wrongly and as a result the changes were not accepted during the merge. The following image shows what happened:

               -----
              (  H  )
               -----
                 ^
                 |
                 |
               -----
              (  M  )
               -----^
                 ^   \
                 |    \  -Delta
                 |     \
                 |     -----
                 |    (  B  )
                 |     -----
                 |    ^
                 |   /  +Delta
               -----/
              (  A  )
               -----
The file was changed in commit B and the changes were undone in commit M.

My problem is how to view the changes to the file between a specific commit, e.g. B and another commit in its "downstream", e.g. H.

If you do
    git log -u B..H

you won't see any changes, as the changes due to M are suppressed. You can see potential changes by doing:

    git log -u --full-history B..H

which will show you that there was a merge commit, but it still won't show you the diff between B and M.

So is there a way of seeing all changes made to a file including those made by a merge commit?

Thanks!
Dov
Jeff King· Sep 9, 2015, 09:04 UTC · re: Dov Grobgeld · lore
On Wed, Sep 09, 2015 at 11:26:47AM +0300, Dov Grobgeld wrote:
Show 19 quoted lines
> The file was changed in commit B and the changes were undone in commit M.
> 
> My problem is how to view the changes to the file between a specific
> commit, e.g. B and another commit in its "downstream", e.g. H.
> 
> If you do
> 
>     git log -u B..H
> 
> you won't see any changes, as the changes due to M are suppressed. You
> can see potential changes by doing:
> 
>     git log -u --full-history B..H
> 
> which will show you that there was a merge commit, but it still won't
> show you the diff between B and M.
> 
> So is there a way of seeing all changes made to a file including those
> made by a merge commit?

Try adding "-m". Normally for merges we show nothing, or the combined diff. In the latter case, we consider a file which matches one of its parents to be an uninteresting change. From the perspective of walking back through history, as git-log does, it is impossible to know whether B made a change that was thrown away in the merge, or whether A made a change that we kept. You can only know the answer by walking further back to the merge-base.

Using "-m" will show you the individual diff against each parent. But since it is limited by the pathspec, it will show you only the diff against "B" in this case, which is what you want. Using this recipe to recreate your setup:

    # a boring history with a file...
    git init
    echo base >file
    git add file
    git commit -m base
    
    # and some other commits...
    echo foo >unrelated
    git add unrelated
    git commit -m unrelated
    
    # meanwhile, somebody forked from us and changed the file...
    git checkout -b other HEAD^
    echo change >>file && git commit -am change
    
    # now let's merge it in, undoing the change
    git checkout master
    git merge --no-commit other
    echo base >file
    git add file
    git commit -m merged
I get:
    $ git log -p -m --full-history file
    commit a279eccbb7cbb806dceff4591a1d4be74b060645 (from 36d29fbf1caf762ef2d78e484bfc1287a9df7603)
    Merge: 2d1e518 36d29fb
    Author: Jeff King <peff@peff.net>
    Date:   Wed Sep 9 04:47:26 2015 -0400
    
        merged
    
    diff --git a/file b/file
    index 09025f9..df967b9 100644
    --- a/file
    +++ b/file
    @@ -1,2 +1 @@
     base
    -change
    [...and so on, showing "change" and "base" as you'd expect]

That's pretty good, though if you had other merges in your log output, I expect "-m" would be messy and confusing there.

So I think what you _really_ want is to recompute the original merge and show something more clever. There is an old topic, tr/remerge-diff, which tried to do this. Its output looks the same as what is above, but it would probably do a better job of not making all of the _other_ merges look ugly.

Unfortunately, development stalled on the topic, and it is nowhere close to being merged. I can dig up pointers if you'd like to play with it.

-Peff

← back to recent threads