-->

git: Is there something like per-branch tags?

2019-06-16 23:59发布

问题:

I have some history rewriting to do for which I'd like to keep my original tree intact for now. The rewritten tree should however copy the tags previously used as well. Is there any less manual option than e.g. prepending tag names with the branch name?

回答1:

No, there is nothing like a per-branch tag in git. All branches and tags are just kinds of refs in Git; a ref is just a name that points to a particular revision in the revision history. For instance, if you have devel and master branches, and v1.0 and v2.0 tags, the references would look something like this:

refs/heads/devel ->  *
                    / \
                   *   * <- refs/heads/master
                   |   |
                   *   *
                    \ /
                     * <- refs/tags/v2.0
                     |
                     *
                     |
                     * <- refs/tags/v1.0
                     |
                     *

As you can see, there's nothing tying those tags to any branches; in fact, all of those tags are contained in both the master and devel branches. By looking inside your .git repo, you can see that there is really no more structure to a tag than that; it is just a file containing a SHA-1 referencing a commit within .git/refs, or a line in .git/packed-refs (tags will frequently be in packed-refs because they don't change often, while branches will usually be separate files within git/refs).

So if you want to rewrite history, and preserve the old tags, you will have to rewrite your tag names. As sehe points out, this is done using git filter-branch --tag-name-filter.



回答2:

You can namespace a tag into a directory named after the branch you want the tag to (kind of) belong to, and it doesn't have to be manual:

git-thisBranch() { git symbolic-ref --short HEAD; } 
git-localTag() { 
   local name="$1"; shift;
   git tag $(git-thisBranch)/"$name" "$@";
}