How can I obtain a list of all branches that introduce changes to a certain file?
Context: When starting to edit a certain file it might be interesting to see what unmerged changes exist on the same file.
We can assume that all branches that are already merged are also deleted. However, if we can filter on branches that are ahead of [main] it would be a plus.
I am not aware of a pure git way to do it. However, you can achieve this by a Bash one-liner:
$ git branch --no-merged | xargs -I branch bash -c "git diff --stat master...branch | grep -q rails/apps/main/Gemfile.lock && echo branch"
git branch -a --no-merged will give you all unmerged branches; local and remote (-a)
Pipe that to xargs to run the following command for every line of output and replace branch by the actual branch name (-I branch):
bash -c "..." runs the quoted argument in a new Bash so we can use | and && on the output of the next command instead of the output of xargs.
git diff --stat master...branch lists all files changed by that branch.
Pipe that to grep -q path/to/Gemfile.lock which will either exit with success or failed (-q).
On success (&&) echo the branch name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With