How to Find Branches That Contain a Commit with git branch –contains
To find out which release branches contain a commit that fixes a bug, use git branch --contains <commit>. Only the branches that contain that commit are listed. However, a branch that received the fix through cherry-pick does not appear in the list, even when its content is the same. I checked this with actual output, including how to confirm such a branch.
I tested on Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>), and I fixed the commit dates with environment variables so the output stays the same each time. The remote was a bare repository created in the same environment. I did not test searching on hosting service screens such as GitHub, and I did not test Windows or macOS.
The test repository
I created a commit 8c04aab on main that fixes a login bug. Around it, I created a release branch and a working branch.
$ git log --oneline --graph --all
* 7ed35e0 ログインの不具合を直す
| * e00490f フッターを作る
| * 4c718f3 お知らせ欄を作る
| | * 8f7b506 プロフィール画面を作る
| |/
| * 8c04aab ログインの不具合を直す
|/
| * 4f79ff1 検索欄を作る
|/
* c0bbd46 ログイン画面を作る
* 833e455 トップページを作る
$ git branch -vv
feature/profile 8f7b506 プロフィール画面を作る
feature/search 4f79ff1 検索欄を作る
* main e00490f [origin/main] フッターを作る
release/1.0 7ed35e0 [origin/release/1.0] ログインの不具合を直す
release/1.1 4c718f3 [origin/release/1.1] お知らせ欄を作る
release/1.1 and feature/profile were created from main after the fix. feature/search was created before the fix. release/1.0 was also created before the fix, and I later took the fix in with git cherry-pick -x. For this reason, there are two commits with the same subject, 8c04aab and 7ed35e0.
$ git log --oneline --grep="不具合" --all
7ed35e0 ログインの不具合を直す
8c04aab ログインの不具合を直す
Listing branches that contain a commit with –contains
Put the hash of the fix commit after --contains. --no-contains does the opposite and lists the branches that do not contain the commit.
$ git branch --contains 8c04aab
feature/profile
* main
release/1.1
$ git branch --no-contains 8c04aab
feature/search
release/1.0
The official git branch reference describes --contains as listing branches whose tip commit is a descendant of the specified commit. In other words, these are the branches where following the parents back from the tip reaches 8c04aab.

You can also give a commit as main~2 (two commits before main) instead of a hash. A shortened hash such as 8c04 gave the same result.
$ git branch --contains main~2
feature/profile
* main
release/1.1
$ git branch --contains 8c04
feature/profile
* main
release/1.1
If you pass a hash that does not exist, you get an error instead of a list.
$ git branch --contains 1234567; echo "終了コード: $?"
error: malformed object name 1234567
終了コード: 129
If you omit the value after --contains, the tip of the current branch (HEAD) is used. When I ran it while on feature/profile, only feature/profile was listed, because the other branches do not contain the tip commit 8f7b506 of feature/profile.
$ git switch -q feature/profile && git branch --contains
* feature/profile
Narrowing the list to release branches
Passing a pattern to --list filters by name. This is useful when you only want to see the release branches.
$ git branch --contains 8c04aab --list "release/*"
release/1.1
--contains and --no-contains can be combined. The next example lists the branches that contain the fix but do not contain the latest commit of main, e00490f. These are the branches that split off from main after the fix and have not received main's latest commit.
$ git branch --contains 8c04aab --no-contains main
feature/profile
release/1.1
When you give --contains twice, all branches that contain either commit are listed. The list is not limited to branches that contain both.
$ git branch --contains 8c04aab --contains feature/search
feature/profile
feature/search
* main
release/1.1
Searching remote branches and tags
-r searches the remote-tracking branches (origin/…), and -a searches both local and remote branches.
$ git branch -r --contains 8c04aab
origin/HEAD -> origin/main
origin/main
origin/release/1.1
$ git branch -a --contains 8c04aab
feature/profile
* main
release/1.1
remotes/origin/HEAD -> origin/main
remotes/origin/main
remotes/origin/release/1.1
Remote-tracking branches show the state at the last git fetch. I pushed a release/1.2 that contains the fix to the remote from another clone, then ran the command before fetching. origin/release/1.2 did not appear in the list.
$ git branch -r --contains 8c04aab
origin/HEAD -> origin/main
origin/main
origin/release/1.1
$ git fetch -q && git branch -r --contains 8c04aab
origin/HEAD -> origin/main
origin/main
origin/release/1.1
origin/release/1.2
To find which version a fix is included in, you can search tags with git tag --contains (official git tag reference). This repository has three tags: v1.0.0 and v1.0.1 on release/1.0, and v1.1.0 on release/1.1.
$ git tag
v1.0.0
v1.0.1
v1.1.0
$ git tag --contains 8c04aab
v1.1.0
v1.0.1 was tagged after I took the fix in with cherry-pick, but it does not appear in the list. The reason is explained in the next section.
Branches that got the fix through cherry-pick do not appear in –contains
The tip of release/1.0 is the commit created when I took 8c04aab in with git cherry-pick -x. Its content is the same fix, but its hash is 7ed35e0, which is a different commit. Following release/1.0 back never reaches 8c04aab, so it does not appear for --contains 8c04aab. Searching with --contains 7ed35e0 lists only release/1.0.
$ git branch --contains 7ed35e0
release/1.0
When you cherry-pick with -x, a line in the form "(cherry picked from commit …)" is added to the end of the commit message, containing the hash of the original commit (official git cherry-pick reference). This line uses the full hash of the original commit.
$ git log -1 --format=%B release/1.0
ログインの不具合を直す
(cherry picked from commit 8c04aabb9498af102ab6d16fd7ac4d783a71801e)
So you can find the cherry-picked commit by searching for this line with git log --grep. git rev-parse converts a short hash into the full hash.
$ git log --oneline --grep="cherry picked from commit $(git rev-parse 8c04aab)" --all
7ed35e0 ログインの不具合を直す
If you cherry-pick without -x, this line is not added. The official git cherry-pick reference also says that even with -x, this line is not added for a cherry-pick that stopped because of a conflict. In that case, use git cherry to check whether a commit with the same change exists. According to the official git cherry reference, git cherry compares the diffs with whitespace and line numbers ignored, and finds commits that were copied by cherry-pick or rebase. If an identical change exists, it prints - in front; otherwise it prints +.
git cherry -v <branch to search> 8c04aab 8c04aab~1 checks only 8c04aab. The commit given as the third argument and the commits before it are excluded from the check.
$ git cherry -v release/1.0 8c04aab 8c04aab~1
- 8c04aabb9498af102ab6d16fd7ac4d783a71801e ログインの不具合を直す
$ git cherry -v feature/search 8c04aab 8c04aab~1
+ 8c04aabb9498af102ab6d16fd7ac4d783a71801e ログインの不具合を直す
release/1.0 has the same change (-), and feature/search does not (+). Running this check on each branch that --no-contains listed confirms them all at once.
$ for b in $(git branch --no-contains 8c04aab --format="%(refname:short)"); do echo "$b: $(git cherry $b 8c04aab 8c04aab~1)"; done
feature/search: + 8c04aabb9498af102ab6d16fd7ac4d783a71801e
release/1.0: - 8c04aabb9498af102ab6d16fd7ac4d783a71801e
The branches that contain the fix are the ones listed by --contains together with the ones marked - here. In this example, only feature/search does not have the fix.




