有没有办法让我的本地 git 标签与远程标签同步?也就是说——不仅在创建时获得新标签(像往常一样,在 fetch
-ing/pull
-ing 时),而且还会修剪不再在远程上的标签,并且当其他人 git push -f
的标签时删除现有标签。
我知道我可以 git fetch remotename
后跟 git remote prune remotename
来为分支实现类似的行为。
最佳答案
...also prune tags no longer on a remote
git fetch
使用 Git 2.17(2018 年第 2 季度)获取
摆脱本地持有的陈旧标签。
参见 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 (2018 年 2 月 9 日)Ævar Arnfjörð Bjarmason (avar
) .
(由 Junio C Hamano -- gitster
-- merge 于 commit c1a7902 ,2018 年 3 月 6 日)
fetch: add a
--prune-tags
option andfetch.pruneTags
config
Add 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 feature it was painful to support the use-case of pulling from a repo which is having both its branches and tags 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 lots of those, so they're archived within weeks for performance reasons.
Without this change it'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.
2021 年 4 月更新,适用于 Windows 的 Git 2.30.1 和 GitHub Desktop 2.8
如果您设置了 prune
和 pruneTags
选项,它将起作用:
cd C:\path\to\local\repo
git config fetch.prune true
git config fetch.pruneTags true
然后在 GitHub Deskop 中单击 Fetch origin:日志将显示:
2021-04-28T20:25:21.244Z - info: [ui] Executing fetch:
git -c credential.helper= -c protocol.version=2 fetch --progress --prune origin (took 2.986s)
...并且远程中不存在的任何本地标签都将消失!
关于git - 在 git 中,如何将我的标签与远程服务器同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10491146/