在 git 中,如何将我的标签与远程服务器同步?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10491146/
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
In git, how do I sync my tags against a remote server?
提问by mlb
Is there a way to keep my local git tags in lockstep with a remote's tags? That is -- not only get new tags when created (as usual, when fetch
-ing/pull
-ing), but also prune tags no longer on a remote and also delete existing tags when someone else git push -f
's a tag.
I know I can git fetch remotename
followed by git remote prune remotename
to achieve similar behaviour for branches.
有没有办法让我的本地 git 标签与远程标签保持同步?也就是说——不仅在创建时获得新标签(像往常一样,当fetch
-ing/ pull
-ing 时),而且还修剪不再在远程上的标签,并在其他人git push -f
的 sa 标签时删除现有标签。我知道我可以git fetch remotename
遵循git remote prune remotename
为分支实现类似的行为。
采纳答案by VonC
...also prune tags no longer on a remote
...也修剪不再在遥控器上的标签
git fetch
gets with Git 2.17 (Q2 2018) an handy short-hand for getting
rid of stale tags that are locally held.
git fetch
使用 Git 2.17(2018 年第二季度)获得了一个方便的简写,用于摆脱本地持有的陈旧标签。
See commit 6317972, commit 97716d2, commit e249ce0, commit 627a129, commit d0e0747, commit 2c72ed7, commit e1790f9, commit 59caf52, commit 82f34e0, commit 6fb23f5, commit ca3065e, commit bf16ab7, commit eca142d, commit 750d0da, commit 0711883, commit ce3ab21, commit aa59e0e(09 Feb 2018) by ?var Arnfj?re Bjarmason (avar
).
(Merged by Junio C Hamano -- gitster
--in commit c1a7902, 06 Mar 2018)
见犯6317972,提交97716d2,提交e249ce0,提交627a129,提交d0e0747,提交2c72ed7,提交e1790f9,提交59caf52,提交82f34e0,提交6fb23f5,提交ca3065e,提交bf16ab7,提交eca142d,提交750d0da,提交0711883,提交ce3ab21,提交aa59e0e(2018 年 2 月 9 日)作者?var Arnfj?re Bjarmason ( avar
)。
(合并于Junio C gitster
Hamano -- --在提交 c1a7902 中,2018 年 3 月 6 日)
fetch: add a
--prune-tags
option andfetch.pruneTags
configAdd a
--prune-tags
option togit-fetch
, along withfetch.pruneTags
config option and a-P
shorthand (-p
is--prune
).
This allows for doing any of:git fetch -p -P git fetch --prune --prune-tags git fetch -p -P origin git fetch --prune --prune-tags origin
Or simply:
git config fetch.prune true && git config fetch.pruneTags true && git fetch
Instead of the much more verbose:
git fetch --prune origin 'refs/tags/*:refs/tags/*' '+refs/heads/*:refs/remotes/origin/*'
Before this featureit was painful to support the use-case of pulling from a repo which is having both its branches andtags deleted regularly, and have our local references to reflect upstream.
At work we create deployment tags in the repo for each rollout, and there's lotsof those, so they're archived within weeks for performance reasons.
Without this changeit's hard to centrally configure such repos in
/etc/gitconfig
(on servers that are only used for working with them). You need to setfetch.prune=true
globally, and then for each repo:git -C {} config --replace-all remote.origin.fetch "refs/tags/*:refs/tags/*" "^\+*refs/tags/\*:refs/tags/\*$"
Now I can simply set
fetch.pruneTags=true
in/etc/gitconfig
as well, and users running "git pull
" will automatically get the pruning semantics I want.
fetch: 添加一个
--prune-tags
选项和fetch.pruneTags
配置添加一个
--prune-tags
选项git-fetch
,以及fetch.pruneTags
配置选项和-P
简写(-p
is--prune
)。
这允许执行以下任何操作:git fetch -p -P git fetch --prune --prune-tags git fetch -p -P origin git fetch --prune --prune-tags origin
或者干脆:
git config fetch.prune true && git config fetch.pruneTags true && git fetch
而不是更冗长:
git fetch --prune origin 'refs/tags/*:refs/tags/*' '+refs/heads/*:refs/remotes/origin/*'
在此功能之前,支持从定期删除其分支和标签的 repo 中提取的用例是很痛苦的,并让我们的本地引用反映上游。
在工作中,我们为每次部署在 repo 中创建部署标签,并且有很多标签,因此出于性能原因,它们会在几周内存档。
如果没有此更改,则很难集中配置此类存储库
/etc/gitconfig
(在仅用于使用它们的服务器上)。您需要fetch.prune=true
全局设置,然后针对每个 repo:git -C {} config --replace-all remote.origin.fetch "refs/tags/*:refs/tags/*" "^\+*refs/tags/\*:refs/tags/\*$"
现在,我可以简单的设置
fetch.pruneTags=true
中/etc/gitconfig
,以及,用户运行“git pull
”会自动获得修剪语义我想要的。
回答by Dominik Ehrenberg
The following worked for me:
以下对我有用:
git fetch --prune --tags
回答by mlb
A bit of research has shown that githas no way to tell the difference between local or foreign tags (all tags go to .git/refs/tags/). Therefore, it is not possible to determine the difference between locally created tags and prune-able remote tags. The options, are then reduced to: having an ever growing set of tags, or only the tags that are on the server.
一些研究表明,git无法区分本地或外部标签(所有标签都转到 .git/refs/tags/)。因此,无法确定本地创建的标签和可修剪的远程标签之间的区别。然后选项被简化为:拥有一组不断增长的标签,或者只有服务器上的标签。
git push --tags origin && \
git tag | xargs -n1 git tag -d && \
git fetch --tags
Drop the first line for the latter behaviour, and could be potentially git alias'd for frequent usage.
删除后一种行为的第一行,并且可能会频繁使用 git 别名。
An alternative would be to create a branch (as they can be identified as local/remote) at a tag point and are never write to it again. Then using remotename/branchname as a tag to checkout would keep tags in sync (in addition to git fetch
and git remote prune remotename
).
另一种方法是在标记点创建一个分支(因为它们可以被识别为本地/远程)并且永远不会再次写入它。然后使用 remotename/branchname 作为标签来结账将使标签保持同步(除了git fetch
和git remote prune remotename
)。
Either way is a hack, and the "right" answer is to stop changing tags all the time.
无论哪种方式都是一种黑客行为,“正确”的答案是一直停止更改标签。
回答by Ron
Another solution which actually works for me:
另一个对我有用的解决方案:
git tag -l | xargs git tag -d && git fetch -t
回答by LJHarb
git push --tags will push your local tags up to the server. By default, git fetch (the first half of git pull or git pull --rebase) will pull tags, but you can specify -t or --tags to pull all of them.
git push --tags 会将您的本地标签推送到服务器。默认情况下,git fetch(git pull 或 git pull --rebase 的前半部分)将拉取标签,但您可以指定 -t 或 --tags 来拉取所有标签。
I'm not sure how to prune remotely deleted tags, but the fetch should pull down any force-updated tags.
我不确定如何修剪远程删除的标签,但提取应该拉下任何强制更新的标签。
回答by Anthony Sottile
disclaimerthis uses git internals (some may argue that the filesystem isa git interface, but that's for another day :D)
免责声明这使用了 git 内部结构(有些人可能会争辩说文件系统是一个 git 接口,但那是另一天:D)
# Blow away all local tags, this will remove any that are tagged locally
# but are not on the remote
rm .git/refs/tags/*
# Download all the tags from the remote
git fetch --tags
回答by xxy
use these command to sync tags(delete all local then fetch all remote)
使用这些命令同步标签(删除所有本地然后获取所有远程)
git tag -d $(git tag) # delete all local tags
git fetch --all # fetch all remote to local
回答by solstice333
Here's an alternative solution:
这是一个替代解决方案:
git fetch -p +refs/tags/*:refs/tags/*
From the git fetchdoc:
从git fetch文档:
-p --prune
Before fetching, remove any remote-tracking references that no longer exist on the remote. Tags are not subject to pruning if they are fetched only because of the default tag auto-following or due to a --tags option. However, if tags are fetched due to an explicit refspec(either on the command line or in the remote configuration, for example if the remote was cloned with the --mirror option), then they are also subject to pruning.
-p --修剪
在获取之前,删除远程上不再存在的任何远程跟踪引用。如果标签仅由于默认标签自动跟随或由于 --tags 选项而被提取,则标签不会受到修剪。但是,如果由于明确的 refspec(在命令行上或在远程配置中,例如,如果远程是使用 --mirror 选项克隆的)而获取标签,则它们也会受到 pruning。