{"thread":{"id":"52489","subject":"How to determine a branch whether has a specified commit","startedAt":"2019-12-19T02:42:21Z","lastAt":"2019-12-19T04:50:10Z","messageCount":3,"participants":["wuzhouhui","Bryan Turner","Jeff King"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"388497","messageId":"62a8f522.6873.16f1c0243f6.Coremail.wuzhouhui14@mails.ucas.ac.cn","threadId":"52489","inReplyTo":null,"subject":"How to determine a branch whether has a specified commit","fromName":"wuzhouhui","fromEmail":"wuzhouhui14@mails.ucas.ac.cn","sentAt":"2019-12-19T02:35:08Z","receivedAt":"2019-12-19T02:42:21Z","isPatch":false,"sender":{"key":"wuzhouhui14@mails.ucas.ac.cn","avatar":null},"body":"Hi,\n\nI know\n    git branch --contains <commit>\ncan display all local branches that has specified commit. However, specify a\nbranch and commit, I don't know how to determine whether this branch contains\nthis commit.\n\nI think\n    git branch --contains <commit> | grep <branch name>\nis not good enough. Does anyone have a better solution?\n\nThanks.\n"},{"id":"388499","messageId":"CAGyf7-GK4SJ8oqJ-n1gCdNOq0Dwsxy4V7Eep7qXTsd51pVz7rA@mail.gmail.com","threadId":"52489","inReplyTo":"62a8f522.6873.16f1c0243f6.Coremail.wuzhouhui14@mails.ucas.ac.cn","subject":"Re: How to determine a branch whether has a specified commit","fromName":"Bryan Turner","fromEmail":"bturner@atlassian.com","sentAt":"2019-12-19T03:09:36Z","receivedAt":"2019-12-19T03:09:49Z","isPatch":false,"sender":{"key":"bturner@atlassian.com","avatar":"https://gravatar.com/avatar/16bcf3167981c1ef7c804e502642366d888a35b0d0b0a4ca01fdc442aa1acb1e?d=mp&s=160"},"body":"On Wed, Dec 18, 2019 at 6:42 PM wuzhouhui <wuzhouhui14@mails.ucas.ac.cn> wrote:\n>\n> Hi,\n>\n> I know\n>     git branch --contains <commit>\n> can display all local branches that has specified commit. However, specify a\n> branch and commit, I don't know how to determine whether this branch contains\n> this commit.\n>\n> I think\n>     git branch --contains <commit> | grep <branch name>\n\n\"Combine\" the two commands: git branch --contains <commit> <branch\nname>. That will either output the branch's name, if it contains the\ncommit, or nothing if it doesn't.\n\nA different approach would be something like: git rev-list --count\n<commit> ^<branch name>. If that returns a count of 0, it means the\ncommit is reachable from the branch. Otherwise, it'll output some\npositive count indicating how many commits (including the specific\ncommit you asked about) are not reachable from the branch. (I suspect\nyou don't really need the count, so you could also use \"-1\" instead of\n\"--count\".)\n\nHope this helps,\nBryan\n"},{"id":"388503","messageId":"20191219045007.GA89333@coredump.intra.peff.net","threadId":"52489","inReplyTo":"CAGyf7-GK4SJ8oqJ-n1gCdNOq0Dwsxy4V7Eep7qXTsd51pVz7rA@mail.gmail.com","subject":"Re: How to determine a branch whether has a specified commit","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2019-12-19T04:50:07Z","receivedAt":"2019-12-19T04:50:10Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Dec 18, 2019 at 07:09:36PM -0800, Bryan Turner wrote:\n\n> > I think\n> >     git branch --contains <commit> | grep <branch name>\n> \n> \"Combine\" the two commands: git branch --contains <commit> <branch\n> name>. That will either output the branch's name, if it contains the\n> commit, or nothing if it doesn't.\n\nOr \"git for-each-ref --contains <commit> refs/heads/<branch>\" if you're\nscripting.\n\n> A different approach would be something like: git rev-list --count\n> <commit> ^<branch name>. If that returns a count of 0, it means the\n> commit is reachable from the branch. Otherwise, it'll output some\n> positive count indicating how many commits (including the specific\n> commit you asked about) are not reachable from the branch. (I suspect\n> you don't really need the count, so you could also use \"-1\" instead of\n> \"--count\".)\n\nThat works, but is not quite as careful as some other techniques with\nrespect to looking past commits with clock skew. A more careful way is\nto check that \"git merge-base <commit> <branch>\" is equal to <commit>.\n\nThat clock-skew thing will eventually go away once we start using\ngeneration numbers consistently (but I don't think we do yet in\nlimit_list(), which handles the \"^\" part of the traversal).\n\n-Peff\n"}]}