30

If there are tags in the remote repository, I'm usually getting them automatically when pulling. When I delete the created local tag (git tag -d <tag-name>) and pull, the deleted tag will be recreated. I can delete remote branches/tags (git push <remote-branch/tag-name>:<branch/tag-name>), but how can I detect that the local tag was created by fetching a remote tag?

1
  • 2
    If you're in a friendly place, the tags would be annotaed, and the ones you've fetched from elsewhere would generally have authors that aren't you. Commented Mar 31, 2011 at 16:14

3 Answers 3

32

If you're annoyed about these tags being recreated when you run git pull, you turn off the fetching of tags by default with the remote.<remote-name>.tagopt config setting. e.g. if the remote is origin, then you can do:

git config remote.origin.tagopt --no-tags 

Update: to address your comment, the reason that I suggest this is that there's not an obvious way to tell the difference between a tag that was created locally and one that was fetched from a remote. There's also no reflog for tags. So, my suggestion is to suppress automatic fetching of tags - you can then fetch them yourself into a different namespace. For example, you could do:

git fetch origin +refs/tags/*:refs/tags/origin/* 

... and perhaps create an alias for that. Then when you want to fetch tags, they'll be named, for example, refs/tags/origin/tag1 instead of refs/tags/tag1.


If you want this to happen automatically, you could change your .git/config to list multiple refspecs for fetching, e.g.:

 [remote "origin"] url = whoever@whereever:whatever.git fetch = +refs/heads/*:refs/remotes/origin/* fetch = +refs/tags/*:refs/tags/origin/* 

... which is suggested in Pro Git.

Sign up to request clarification or add additional context in comments.

7 Comments

@mklhmnn: my idea was that you can suppress automatic fetching of tags, and then when you want the remote tags, fetch them into a different namespace. I've updated my answer with an example.
@Mark: I find the approach much to the point than mine, separating tags by namespace on fetch. I would +1, but I already did before your edit ;)
Alternatively, you could simply create local tags in a different namespace.
Thanks, Mark and VonC. So the answer is: no, one can't distinguish between locally created tags and tags created from a remote, isn't it?
Is there a way to add that fetch line in your .git/config without manually editing the .git/config file? I'm looking for something like "git remote set-branches --add ..." but it always prepends refs/heads. Also, the Pro Git book doesn't seem to mention tags at all in the page you link... it would've been probably better to also quote the relevant bit at the time :)
|
10

a tag isn't "local" or "remote": it is associated to a commit, which can part of multiple branches, including ones in the remotes namespace.

Get tag SHA1 of the commit referenced by a tag

git show -s 'TAG_NAME^{commit}' --format='%H' 

, and do a :

git branch -a --contains SHA1 

If you see

 remotes/aRemoteRepoName/aBranch 

you know that tag references a commit you have fetched from a remote repo.

As Chris mentions:

git branch -a --contains TAGNAME 

will dereference the tag and gives the answer in one go.

4 Comments

Why not all in one command: git branch -a --contains 'TAG_NAME^{commit}?
Thinking about it some more, --contains will automatically dereference the named object, so there is no need to manually specify the dereferencing either. Simply git branch -a --contains TAG_NAME works as expected.
I don't want to know whether a tag is located in the history of a branch, I want to know whether the tag was created by fetch or manually.
+1 for the first sentence, which answers the question. Thank you. (I agree with @Mot, I don't want to know about history, I want to know whether the tag was created locally or whether it was pulled from the remote. So without semi-destructive solutions like deleting all tags and re-fetching, there is no way.)
3

As long as you currently have access to the remote repository, you can compare the results of

git ls-remote --tags 

with

git show-ref --tags -d 

Note:

  • The remote must currently be available
  • If the tag was deleted from the remote, you wouldn't be able to tell that the "local" tag was originally pulled from the remote
  • This will allow you to see when the remote and local have the same tag name but either reference different commits or were created/updated independently (for annotated tags, the tag hashes would be different even if the commit hash was the same)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.