threads / discuss / 52489

How to determine a branch whether has a specified commit

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

## tl;dr

3 messages between Dec 19, 2019 and Dec 19, 2019.

replies: 2people: 3as markdown or json

wuzhouhui· Dec 19, 2019, 02:35 UTC · lore
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>
is not good enough. Does anyone have a better solution?
Thanks.
Bryan Turner· Dec 19, 2019, 03:09 UTC · re: wuzhouhui · lore

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

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

Jeff King· Dec 19, 2019, 04:50 UTC · re: Bryan Turner · lore

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

On Wed, Dec 18, 2019 at 07:09:36PM -0800, Bryan Turner wrote:
Show 6 quoted lines
> > 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.

Or "git for-each-ref --contains <commit> refs/heads/<branch>" if you're scripting.

Show 7 quoted lines
> 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".)

That works, but is not quite as careful as some other techniques with respect to looking past commits with clock skew. A more careful way is to check that "git merge-base <commit> <branch>" is equal to <commit>.

That clock-skew thing will eventually go away once we start using generation numbers consistently (but I don't think we do yet in limit_list(), which handles the "^" part of the traversal).

-Peff

← back to recent threads