How to find git branches which contain a specific commit

How to find git branches which contain a specific commit

Photo by Suganth on Unsplash

I recently wanted to find out whether or not a specific commit was in a git branch. (The branch contained the code which was formed the most recent deployment to production, and I wanted to check whether or not the commit I was looking at had been deployed.)

Thanks to Stack Overflow I was able to find a git command that does exactly what I need. It turns out that the git branch command, which lists git branches, can take a --contains <commit-id> flag that filters the list of branches to only those which contain the specified commit. This was perfect for what I needed.

This is the command in full, with the necessary modification for changing whether you get local, remote or all branches.

Local branches only:

git branch --contains <commit-id>

Remote branches only:

git branch -r --contains <commit-id>

Both local and remote branches:

git branch -a --contains <commit-id>