Tag a git commit with a “tag” tag

2019-07-28 15:03发布

问题:

I have used the following command to tag my HEAD with a tag named "tag":

git tag -a tag -m "comment on my tag"

But when I

git push origin tag

I get an error:

fatal: tag shorthand without < tag >

I don't get the same error for tag with different name. I suppose git treats this "tag" as its sub-command. Maybe it is not an often use case... but is it possible to push "tag" to a remote repo? I wouldn't like to push my other tags with

git push --tags

though!

回答1:

If you take a look at the git code (link below), we can see that during a push it is checking for keyword tag.

https://github.com/tnachen/git/blob/master/builtin/push.c

Short answer: Give the tag a meaningful name and do not use git keywords

static void set_refspecs(const char **refs, int nr)
{
    int i;
    for (i = 0; i < nr; i++) {
        const char *ref = refs[i];
        if (!strcmp("tag", ref)) {
            char *tag;
            int len;
            if (nr <= ++i)
                die("tag shorthand without <tag>");
            len = strlen(refs[i]) + 11;
            if (deleterefs) {
                tag = xmalloc(len+1);
                strcpy(tag, ":refs/tags/");
            } else {
                tag = xmalloc(len);
                strcpy(tag, "refs/tags/");
            }
            strcat(tag, refs[i]);
            ref = tag;
        } else if (deleterefs && !strchr(ref, ':')) {
            char *delref;
            int len = strlen(ref)+1;
            delref = xmalloc(len+1);
            strcpy(delref, ":");
            strcat(delref, ref);
            ref = delref;
        } else if (deleterefs)
            die("--delete only accepts plain target ref names");
        add_refspec(ref);
    }
}


标签: git tags push