4

I have used following command to fetch a remote tag, it completed successfully by fetching the remote objects.

git fetch origin <tag name >

After that gave following command to switch to tag, but it gave error: pathspec error.

git checkout <tag name>

Now I tried following command.

git fetch --tags

After that the command, git checkout <tag name> executed successfully.

Can you help to understand during git fetch origin <tag name> why the tag was not fetched.

Ravi A
  • 421
  • 2
  • 9
  • 21

1 Answers1

7

The git fetch documentation does mention:

By default, tags that point at objects that are downloaded from the remote repository are fetched and stored locally

However: if you are fetching a refspec referencing a tag name directly, you are actually fetching what the tag points to, but: you don't specify the refspec destination.
So you see only:

 * tag aTag -> FETCH_HEAD

The tag reference is copied to .git/FETCH_HEAD, but not created locally.

As opposed to git fetch --tags, which is the equivalent of using the refspec refs/tags/*:refs/tags/*: source (the tags/*) and destination (in your repo: tags/*) are specified, the remote tags are created locally.

See more at "A git tag doesn't seem to be available after explicitly fetching the tag"

A git fetch origin refs/tags/<tag name>:refs/tags/<tag name> would have worked.
The git checkout <tag name> would not have yield "error: pathspec '<tag name>' did not match any file(s) known to git"

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I have cloned `https://github.com/Azure/azure-iot-sdk-c.git` and tried to fetch the tag `git fetch refs/tag/1.3.4:refs/tag/1.3.4`,but it is giving error. The clone command and fetch command outputs are [pasted](https://pastebin.com/W3VtB6ea) – Ravi A Oct 27 '19 at 17:55
  • What does a git fetch --tags, followed by git tag -l returns? Do you see your 1.3.4 tag in there? – VonC Oct 27 '19 at 17:58
  • `git tag -l` lists the tag 1.3.4. The command output [pasted](https://pastebin.com/H0w4Qj5w) – Ravi A Oct 27 '19 at 18:04
  • I have added `fetch = +refs/tags/*:refs/remotes/origin/tags/*` in .git/config then `git fetch origin 1.3.4` was successful, [output](https://pastebin.com/h3Pc4kHM). Before modifying `.git/config` file, I have tried with `git fetch refs/tags/1.3.4:refs/remotes/origin/tags/1.3.4`, but it gave error `fatal: 'refs/tags/1.3.4:refs/remotes/origin/tags/1.3.4' does not appear to be a git repository`. – Ravi A Oct 27 '19 at 18:16
  • 1
    @RaviA It was `git fetch refs/tags/:refs/tags/`, with `tags/`, not `tag/`. I have edited the answer accordingly. – VonC Oct 27 '19 at 20:59
  • Thanks it worked, but for me it expects `origin` also along with `tags` i.e `git fetch origin refs/tags/1.3.4:refs/tags/1.3.4`. May be my internal settings issue due to which it is expecting remote repository name `origin` also. – Ravi A Oct 28 '19 at 02:20