git: Is there something like per-branch tags?
Asked Answered
L

2

12

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?

Ledeen answered 18/4, 2011 at 12:58 Comment(0)
K
20

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.

Kathlyn answered 18/4, 2011 at 14:37 Comment(1)
@sehe: thanks, that reminds me to improving this scriptLedeen
M
3

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" "$@";
}
Megasporangium answered 8/11, 2015 at 16:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.