Git:区分本地和远程标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5496386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git: distinguish between local and remote tags
提问by Mot
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?
如果远程存储库中有标签,我通常会在拉取时自动获取它们。当我删除创建的本地标签 ( git tag -d <tag-name>
) 并拉取时,将重新创建已删除的标签。我可以删除远程分支/标签 ( git push <remote-branch/tag-name>:<branch/tag-name>
),但是如何检测本地标签是通过获取远程标签创建的?
回答by Mark Longair
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>.tagoptconfig setting. e.g. if the remote is origin
, then you can do:
如果您对运行时重新创建这些标签感到恼火git pull
,默认情况下,您可以使用remote.<remote-name>.tagopt配置设置关闭标签的获取。例如,如果遥控器是origin
,那么您可以执行以下操作:
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:
更新:为了解决您的评论,我建议这样做的原因是没有明显的方法来区分本地创建的标签和从远程获取的标签之间的区别。也没有reflog
标签。所以,我的建议是禁止自动获取标签 - 然后您可以自己将它们提取到不同的命名空间中。例如,你可以这样做:
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
.
...也许为此创建一个别名。然后,当您想要获取标签时,它们将被命名,例如,refs/tags/origin/tag1
而不是refs/tags/tag1
.
If you want this to happen automatically, you could change your .git/config
to list multiple refspecs for fetching, e.g.:
如果您希望这自动发生,您可以更改您.git/config
以列出多个用于获取的引用规范,例如:
[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.
...这是在 Pro Git 中建议的。
回答by VonC
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
获取标签引用的提交的标签 SHA1
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 Chrismentions:
正如克里斯所说:
git branch -a --contains TAGNAME
will dereference the tag and gives the answer in one go.
将取消引用标签并一次性给出答案。