我们在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
其他回答
更新@2021/05
将$REPO参数传递给自定义脚本。
sync_git_tags.sh的内容
#!/bin/sh
# cd to $REPO directory
cd $1
pwd
# sync remote tags
git tag -l | xargs git tag -d && git fetch -t
Old
ps:更新@2021/05,git获取-修剪-修剪标签来源不工作在我的MacOS。
我将该命令作为MacOS上的自定义操作添加到SourceTree。 通过Sourcetree -> Preferences设置自定义操作…->自定义动作
我使用git fetch -prune- prune-tags origin来同步标签从远程到本地。
这是一个很好的方法:
Git标签-l | xargs Git标签-d && Git获取-t
来源:demisx.GitHub.io
这是个好问题,我也一直在想同样的问题。
我不想写一个脚本,所以寻求一个不同的解决方案。关键是发现可以在本地删除标记,然后使用git fetch从远程服务器“取回”它。如果标签在远程上不存在,那么它将保持删除状态。
因此你需要按顺序输入两行:
git tag -l | xargs git tag -d
git fetch --tags
这些:
从本地回收中删除所有标签。总之,xargs将每个通过“tag -l”输出的标记放到“tag -d”的命令行上。没有这个,git不会删除任何东西,因为它不读取stdin(愚蠢的git)。 从远程回收中获取所有活动标记。
这甚至在Windows上也很有效。
刚刚在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)
这样如何-删除所有本地标签,然后重新获取? 考虑到你的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