简体   繁体   中英

Tag a git commit with a "tag" tag

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!

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);
    }
}

This is what I did:

  • Created a new branch off of the tag branch
  • Switch to the new branch
  • Pushed my code to remote repo

I deleted the tag branch afterwards.

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM