git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: How to determine a branch whether has a specified commit

From
Bryan Turner <bturner@atlassian.com>
Date
Dec 19, 2019, 03:09 UTC
Message-ID
<CAGyf7-GK4SJ8oqJ-n1gCdNOq0Dwsxy4V7Eep7qXTsd51pVz7rA@mail.gmail.com>
In-Reply-To
<62a8f522.6873.16f1c0243f6.Coremail.wuzhouhui14@mails.ucas.ac.cn>
On Wed, Dec 18, 2019 at 6:42 PM wuzhouhui <wuzhouhui14@mails.ucas.ac.cn> wrote:
Show 11 quoted lines
>
> Hi,
>
> I know
>     git branch --contains <commit>
> can display all local branches that has specified commit. However, specify a
> branch and commit, I don't know how to determine whether this branch contains
> this commit.
>
> I think
>     git branch --contains <commit> | grep <branch name>

"Combine" the two commands: git branch --contains <commit> <branch name>. That will either output the branch's name, if it contains the commit, or nothing if it doesn't.

A different approach would be something like: git rev-list --count <commit> ^<branch name>. If that returns a count of 0, it means the commit is reachable from the branch. Otherwise, it'll output some positive count indicating how many commits (including the specific commit you asked about) are not reachable from the branch. (I suspect you don't really need the count, so you could also use "-1" instead of "--count".)

Hope this helps, Bryan

Previous: wuzhouhuiNext: Jeff King
Message 2 of 3 in “How to determine a branch whether has a specified commit”
  1. wuzhouhuiDec 19, 2019
  2. Bryan TurnerDec 19, 2019
  3. Jeff KingDec 19, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.