git log of files that never got merged

Viewed 95

background

I recently had an issue tracking down an accidentally unmerged documentation file during a merge.

For whatever reason I had messed up a merge and effectively deleted the file (and several other files) because I had forgotten they existed.

Now I'd like to look at my history and see if any other merges went wrong.

reproducing issue

git init
echo "file a" > a
git add a
git commit -m "file a added"

git checkout -b branch_c
echo "file c" > c
git add c
git commit -m "file c added"

git checkout master
git checkout -b branch_b
echo "file b" > b
git add b
git commit -m "file b added"

# For whatever reason we forgot to merge file c and forgot about it
git merge --no-commit branch_c
git reset c
git commit -m "merge branch c, but forgot file c"
git clean -fxd
# Commands such as git-log fail to show that files were excluded from merges
git log --name-status --all

question

How can I list files that never got merged into some branch? Is there a way to use git-log so that it shows the files that didn't get merged?

2 Answers

Starting from the script you attached, first thing I came up with was to compare a full merge with the partial merge. Then, depending on how many merge commits you want to check, you can even create a custom script. By the way,

Checkout to first parent of the merge commit you want to check

git checkout branch_b^

Exit from detached head by creating a new branch (temp_branch) and do a full merge.

git checkout -b temp_branch
git merge -m "full merge with all files" branch_c

Now you can just compare the two tree with git diff-tree

git diff-tree --compact-summary HEAD branch_b

That return something like this:

 c (gone) | 1 -
 1 file changed, 1 deletion(-)

Many options other than --compact-summary are available. In the end, delete the temporary branch (git branch -D). Honestly, I do not know if git log is powerful enough to show unmerged files, but this seems a reasonable solution to me.

How can I list files that never got merged into some branch? Is there a way to use git-log so that it shows the files that didn't get merged?

No. The problem is, as far as Git is concerned, those files are merged.

Or, to put it another way, Git doesn't merge files. Git merges commits. Once you've made the final commit result, that result is by definition the correct result of merging all the files contained within the now-merged commits, as far as Git is concerned. That's because Git is about commits, not files.

Marco Luzzara's answer has what you can do: re-perform each merge in question, to see if you get the same result as last time. There's ongoing work in Git that will enable Git to do this for you, in a more automated way, to the extent that Git can (that is, an automated redo of the merge may end up with merge conflicts, and at best Git can just show you the merge-conflicted-combined-version of some file against the committed-version in the merge). But that's not available today, and probably won't be for some time.

Related