How to list branches that contain a given commit?

前端 未结 3 1878
闹比i
闹比i 2020-11-22 02:23

How can I query git to find out which branches contain a given commit? gitk will usually list the branches, unless there are too many, in which case it just say

3条回答
  •  终归单人心
    2020-11-22 03:01

    The answer for git branch -r --contains works well for normal remote branches, but if the commit is only in the hidden head namespace that GitHub creates for PRs, you'll need a few more steps.

    Say, if PR #42 was from deleted branch and that PR thread has the only reference to the commit on the repo, git branch -r doesn't know about PR #42 because refs like refs/pull/42/head aren't listed as a remote branch by default.

    In .git/config for the [remote "origin"] section add a new line:

    fetch = +refs/pull/*/head:refs/remotes/origin/pr/*
    

    (This gist has more context.)

    Then when you git fetch you'll get all the PR branches, and when you run git branch -r --contains you'll see origin/pr/42 contains the commit.

提交回复
热议问题