我们在git中使用标签作为部署过程的一部分。有时,我们希望通过从远程存储库中删除这些标记来清理它们。
这很简单。一个用户在一组命令中删除了本地标签和远程标签。我们有一个结合了这两个步骤的shell脚本。
第二个(第3个,第4个,……)用户现在拥有不再反映在远程上的本地标记。
我正在寻找一个类似于git远程修剪起源的命令,清理本地跟踪分支,其中远程分支已被删除。
或者,可以使用一个简单的命令来列出远程标记,与通过git tag -l返回的本地标记进行比较。
我们在git中使用标签作为部署过程的一部分。有时,我们希望通过从远程存储库中删除这些标记来清理它们。
这很简单。一个用户在一组命令中删除了本地标签和远程标签。我们有一个结合了这两个步骤的shell脚本。
第二个(第3个,第4个,……)用户现在拥有不再反映在远程上的本地标记。
我正在寻找一个类似于git远程修剪起源的命令,清理本地跟踪分支,其中远程分支已被删除。
或者,可以使用一个简单的命令来列出远程标记,与通过git tag -l返回的本地标记进行比较。
当前回答
这样如何-删除所有本地标签,然后重新获取? 考虑到你的repo可能包含子模块:
git submodule foreach --recursive 'git tag | xargs git tag -d'
(alternatively, "for i in `find .git -type d -name '*tags*'`; do rm -f $i/*; done")
git fetch -t
git submodule foreach --recursive git fetch -t
其他回答
刚刚在GitHub上的pivotal_git_scripts Gem fork中添加了git sync-local-tags命令:
https://github.com/kigster/git_scripts
安装gem,然后在存储库中运行"git sync-local-tags"来删除远程服务器上不存在的本地标记。
或者你也可以安装下面这个脚本,并将其命名为"git-sync-local-tags":
#!/usr/bin/env ruby
# Delete tags from the local Git repository, which are not found on
# a remote origin
#
# Usage: git sync-local-tags [-n]
# if -n is passed, just print the tag to be deleted, but do not
# actually delete it.
#
# Author: Konstantin Gredeskoul (http://tektastic.com)
#
#######################################################################
class TagSynchronizer
def self.local_tags
`git show-ref --tags | awk '{print $2}'`.split(/\n/)
end
def self.remote_tags
`git ls-remote --tags origin | awk '{print $2}'`.split(/\n/)
end
def self.orphaned_tags
self.local_tags - self.remote_tags
end
def self.remove_unused_tags(print_only = false)
self.orphaned_tags.each do |ref|
tag = ref.gsub /refs\/tags\//, ''
puts "deleting local tag #{tag}"
`git tag -d #{tag}` unless print_only
end
end
end
unless File.exists?(".git")
puts "This doesn't look like a git repository."
exit 1
end
print_only = ARGV.include?("-n")
TagSynchronizer.remove_unused_tags(print_only)
在新的git版本(如v2.26.2或更高版本)中,您可以使用——prune-tags
- p ——prune-tags 在获取之前,如果启用了——prune,则删除远程上不再存在的任何本地标记。这个选项应该更谨慎地使用,不像——prune它将删除已经创建的任何本地引用(本地标记)。这个选项是提供显式标记refspec和——prune的简写,请参阅其文档中关于它的讨论。
所以你需要运行:
git fetch origin --prune --prune-tags
自v1.7.8以来的所有版本的Git都理解使用refspec来获取Git,而自v1.9.0以来——tags选项覆盖了——prune选项。对于一个通用的解决方案,试试这个:
$ git --version
git version 2.1.3
$ git fetch --prune origin "+refs/tags/*:refs/tags/*"
From ssh://xxx
x [deleted] (none) -> rel_test
有关“——tags”和“——prune”行为在Git v1.9.0中如何改变的进一步阅读,请参见:https://github.com/git/git/commit/e66ef7ae6f31f246dead62f574cc2acb75fd001c
与@Richard W的答案相同,但适用于Windows (PowerShell)
git tag | ForEach-Object -Process { git tag -d $_ }
git fetch -t
看起来Git的最新版本(我使用的是Git v2.20)允许人们简单地说
git fetch --prune --prune-tags
多干净!
https://git-scm.com/docs/git-fetch#_pruning
你也可以配置git在抓取时总是修剪标签:
git config fetch.pruneTags true
如果您只想在从特定的远程获取时修剪标签,您可以使用remote.<remote>。pruneTags选项。例如,当从原点读取而不是从其他遥控器读取时,总是修剪标签,
git config remote.origin.pruneTags true