Search code examples
gitgit-merge

Is it possible to find out which files conflicted in a merge commit after the fact? Or a way to force git to list the files in the commit message?


I would like to know which files git was not able to automatically merge when a merge conflict happened. I found this answer: How to list conflicted files (files with changes in both parents) in a git merge commit?

But just tested the git show --name-status and it didn't work in my case, the files which I know conflicted didn't show up in the list...

Alternatively, I'm wondering if it's possible to force these files to be included in the git commit message when doing a merge commit somehow? Or at least to change the default git behaviour so that the files aren't commented out by default? (to make this change for everyone on my team using a repository obviously)


Solution

  • Yes, but it involves Git hooks which are a hassle (see below). In the rare cases you need such information you can instead recreate the conflict.

    Let's say you're looking at this repo.

    A - B - F - G -- M - N - O [master]
         \         /
          C - D - E
    

    And you want to know about conflicts at M. Checkout G and merge E.

    git checkout G
    git merge E
    

    Any conflicts will be reproduced.


    I would like to know which files git was not able to automatically merge when a merge conflict happened.

    Git will not record which files were conflicted in a merge commit. This information is ephemeral.

    Alternatively, I'm wondering if it's possible to force these files to be included in the git commit message when doing a merge commit somehow? Or at least to change the default git behaviour so that the files aren't commented out by default?

    Yes. However...

    (to make this change for everyone on my team using a repository obviously)

    Not automatically.

    You can use a prepare-commit-msg hook to edit the commit message. The first argument is the original file and the second is the source (you're looking for merge). You can write a hook that either uncomments the existing conflict lines, or write your own file with the list of conflicted files.

    However, Git hooks are not distributed with the repository. Everyone on your team would have to install them and keep them up to date.

    Commit messages cannot be altered on the server side (ie. after pushing) because the commit message is part of the commit. Changing the message would change the commit ID.