git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* How to determine a branch whether has a specified commit
@ 2019-12-19  2:35 wuzhouhui
  2019-12-19  3:09 ` Bryan Turner
  0 siblings, 1 reply; 3+ messages in thread
From: wuzhouhui @ 2019-12-19  2:35 UTC (permalink / raw)
  To: git

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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How to determine a branch whether has a specified commit
  2019-12-19  2:35 How to determine a branch whether has a specified commit wuzhouhui
@ 2019-12-19  3:09 ` Bryan Turner
  2019-12-19  4:50   ` Jeff King
  0 siblings, 1 reply; 3+ messages in thread
From: Bryan Turner @ 2019-12-19  3:09 UTC (permalink / raw)
  To: wuzhouhui; +Cc: Git Users

On Wed, Dec 18, 2019 at 6:42 PM wuzhouhui <wuzhouhui14@mails.ucas.ac.cn> wrote:
>
> 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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How to determine a branch whether has a specified commit
  2019-12-19  3:09 ` Bryan Turner
@ 2019-12-19  4:50   ` Jeff King
  0 siblings, 0 replies; 3+ messages in thread
From: Jeff King @ 2019-12-19  4:50 UTC (permalink / raw)
  To: Bryan Turner; +Cc: wuzhouhui, Git Users

On Wed, Dec 18, 2019 at 07:09:36PM -0800, Bryan Turner wrote:

> > 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.

> 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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-12-19  4:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-19  2:35 How to determine a branch whether has a specified commit wuzhouhui
2019-12-19  3:09 ` Bryan Turner
2019-12-19  4:50   ` Jeff King

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).