Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Show all branches that touch a certain file

Tags:

git

branch

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.

like image 651
jakob-r Avatar asked Oct 31 '25 07:10

jakob-r


1 Answers

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.

like image 184
AmShaegar Avatar answered Nov 03 '25 07:11

AmShaegar