George V. Reilly

Find remote Git branch with change to a file

I needed to track down a remote branch created a couple of months ago on another machine. I knew which file had been changed, but none of the far-too-many remote branches’ names rang a bell.

Turns out that using git branch –contains in the right way finds all the relevant branches.

git log --all --format=%H FILENAME \
    | while read f; do git branch --remotes --contains $f; done \
    | sort -u

The first line, git log --all --format=%H FILENAME, lists all the hashes for commits that contained changes to FILENAME. The second finds all the branches that contain those hashes (I added --remotes). The third uniquifies the results.

The original answer goes on to suggest using gitk --all --date-order -- FILENAME to manually inspect all branches. However, gitk complains unknown color name "lime" on my new Mac. I must have hit this before, on an earlier in­stal­la­tion of Git, because the Stack­Over­flow answer contains an edit from me.

blog comments powered by Disqus
Review: The Mercy of the Night » « Lugging CRT Monitors to MontrĂ©al