问题
So I've read Git: How to find out on which branch tag is? and many other posts and I already know how to use:
git branch --contains tag
However, I still need to know how to list branches that contain a given commit (or tag) in specific repository.
Background
I've extracted a list of tags from a remote repository archive
using:
git ls-remote --tags archive
Now, for each of the tags extracted, I wish to get the list of containing branches.
Using command:
git branch --contains tag
doesn't help because the tags are not found because they do not exists in origin
repository.
回答1:
You need to git branch
--all
--contains
commit
to see branches from other remotes.
This still depends on you fetching the commits you want to examine from whatever remote they're on - git fetch archive
in your case.
Without it your local repository doesn't have the commits, and pretty much nothing will work. For example, you can't git log
etc. without having the commits present. There are all sorts of (semi-working) workarounds, like SSHing to the server and working on it (if it's available) or using all sorts of web APIs (GitHub, Stash) if they're available, but basically you need to the the commits present. git ls-remote
is the exception that can work on a remote repository without having anything locally.
回答2:
Given your stated constraints (specifically, that you have a mapping from tag-name to SHA-1 ID and no other information at all), you cannot get what you want.
The definition of the predicate:
branch name contains commit id
is simply that id is an ancestor1 of the SHA-1 to which name maps. To test this proposition, you must be able to traverse the graph, starting at the node to which name maps and walking back through all paths in the graph until you either run out of nodes, or encounter the given id.
To traverse the graph, though, you must have the graph.
This means there are only two ways to find the answer: have the graph yourself, or get some entity that has the graph to answer the question for you. There is nothing built in to git and git servers to do the latter (though some servers could offer it as an add-on, perhaps).
On the other hand, if you can run git ls-remote
, you can also retrieve the repository, and therefore you can answer the question yourself (at the cost of retrieving the repository, of course).
1For the purpose of this definition, a node is its own ancestor. In this particular degenerate case (where the tag and the tip of the branch match), of course, you can immediately answer the question by looking at the SHA-1 IDs of the branch tips, and these values are available through git ls-remote
.
来源:https://stackoverflow.com/questions/35008886/how-to-list-branches-that-contain-a-given-commit-on-a-specific-repository