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

Re: check if one branch contains another branch

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
May 7, 2020, 23:47 UTC
Message-ID
<20200507234718.GC7234@camp.crustytoothpaste.net>
In-Reply-To
<CA+KyZp4keGp8j0rzCtr0fEogZqkKwTNLvRBZ2QOUzjStP+V2uA@mail.gmail.com>
On 2020-05-07 at 23:28:40, Alexander Mills wrote:
Show 16 quoted lines
> Thanks will check that out.
> 
> This command does not seem to work :(
> 
> > git branch --contains branchB
> 
> I do this:
> 
> git checkout branchB
> git commit --allow-empty -am 'empty commit message'
> git checkout dev
> git branch --contains branchB  ==> exit code 0
> git branch --contains $(git rev-parse branchB)  ==> exit code 0
> 
> this seems like a bug or something.  Why wouldn't it exit with 1,
> since it obviously does not contain that commit?
Because that command operates differently.  From git-branch(1):
  With --contains, shows only the branches that contain the named commit
  (in other words, the branches whose tip commits are descendants of the
  named commit)…..

git branch --contains branchB prints only branchB, because no other branch contains it. In other words, this asks to list the branches which contain the specified commit, and as long as it has done so successfully (even if that answer is "none of them"), it exits 0. The current branch has no effect on it because it's listing branches which match a criterion, not comparing the specified revision to the current branch.

Note that this is not a porcelain command (that is, it is not intended for scripting) and need not be especially performant. git merge-base --is-ancestor is the better way if you want to script things or get a more performant answer because it does less work, especially if there are many branches.

> This kinda sucks tbh :(

Certainly it fails to do the thing you wanted to do with it, but since that isn't what it's documented to do, I don't see that as a particular problem.

Is the documentation unclear in some way or could it be more helpful? If so, please tell us so we can improve it.

-- 
brian m. carlson: Houston, Texas, US
OpenPGP: https://keybase.io/bk2204
Previous: Alexander MillsNext: Randall S. Becker
Message 7 of 12 in “check if one branch contains another branch”
  1. Alexander MillsMay 7, 2020
  2. Junio C HamanoMay 7, 2020
  3. Alexander MillsMay 7, 2020
  4. Alexander MillsMay 7, 2020
  5. brian m. carlsonMay 7, 2020
  6. Alexander MillsMay 7, 2020
  7. brian m. carlsonMay 7, 2020
  8. Randall S. BeckerMay 7, 2020
  9. Alexander MillsMay 7, 2020
  10. Junio C HamanoMay 7, 2020
  11. Alexander MillsMay 8, 2020
  12. Randall S. BeckerMay 8, 2020

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.