删除不再位于远程存储库上的本地 git 标签
我们在 git 中使用标签作为部署过程的一部分。有时,我们希望通过从远程存储库中删除这些标签来清理它们。
这非常简单。一个用户通过一组命令删除本地标签和远程标签。我们有一个结合了这两个步骤的小 shell 脚本。
第二个(第三个、第四个……)用户现在拥有不再反映在远程上的本地标签。
我正在寻找类似于 git Remote prune origin 的命令,该命令可以清理已删除远程分支的本地跟踪分支。
或者,可以使用列出远程标签的简单命令来与通过 git tag -l 返回的本地标签进行比较。
We use tags in git as part of our deployment process. From time to time, we want to clean up these tags by removing them from our remote repository.
This is pretty straightforward. One user deletes the local tag and the remote tag in one set of commands. We have a little shell script that combines both steps.
The 2nd (3rd, 4th,...) user now has local tags that are no longer reflected on the remote.
I am looking for a command similar to git remote prune origin
which cleans up locally tracking branches for which the remote branch has been deleted.
Alternatively, a simple command to list remote tags could be used to compare to the local tags returned via git tag -l
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
这是一个很好的问题,我也一直想知道同样的事情。
我不想编写脚本,因此寻求不同的解决方案。关键是发现您可以在本地删除标签,然后使用 git fetch 从远程服务器“取回”它。如果遥控器上不存在该标签,则它将保持删除状态。
因此,您需要按顺序输入两行:
这些:
从本地存储库中删除所有标签。 FWIW,xargs 将“tag -l”输出的每个标签放置到“tag -d”的命令行上。如果没有这个,git 将不会删除任何内容,因为它不读取标准输入(愚蠢的 git)。
从远程存储库中获取所有活动标签。
这甚至在 Windows 上也能发挥作用。
This is great question, I'd been wondering the same thing.
I didn't want to write a script so sought a different solution. The key is discovering that you can delete a tag locally, then use git fetch to "get it back" from the remote server. If the tag doesn't exist on the remote, then it will remain deleted.
Thus you need to type two lines in order:
These:
Delete all tags from the local repo. FWIW, xargs places each tag output by "tag -l" onto the command line for "tag -d". Without this, git won't delete anything because it doesn't read stdin (silly git).
Fetch all active tags from the remote repo.
This even works a treat on Windows.
看起来最近版本的 Git(我使用的是 git v2.20)可以让人们简单地说“
干净得多”!
https://git-scm.com/docs/git-fetch#_pruning
您还可以将 git 配置为在获取时始终修剪标签:
如果您只想在从特定远程获取时修剪标签,则可以使用
remote..pruneTags
选项。例如,在从源而不是其他远程获取时始终修剪标签,Looks like recentish versions of Git (I'm on git v2.20) allow one to simply say
Much cleaner!
https://git-scm.com/docs/git-fetch#_pruning
You can also configure git to always prune tags when fetching:
If you only want to prune tags when fetching from a specific remote, you can use the
remote.<remote>.pruneTags
option. For example, to always prune tags when fetching from origin but not other remotes,从 Git v1.7.8 到 v1.8.5.6,您可以使用这个:
更新
这不适用于较新版本的 git(从 v1.9.0 开始),因为提交 e66ef7ae6f31f2。我真的不想删除它,因为它确实对某些人有用。
正如“Chad Juliano”所建议的,对于 v1.7.8 以来的所有 Git 版本,您可以使用以下命令:
您可能需要用引号将标签部分括起来(例如在 Windows 上)以避免通配符扩展:
注意:在所有这些中案例可能是“来源”或您通常引用的任何遥控器。
From Git v1.7.8 to v1.8.5.6, you can use this:
Update
This doesn't work on newer versions of git (starting with v1.9.0) because of commit e66ef7ae6f31f2. I don't really want to delete it though since it did work for some people.
As suggested by "Chad Juliano", with all Git version since v1.7.8, you can use the following command:
You may need to enclose the tags part with quotes (on Windows for example) to avoid wildcard expansion:
NOTE: in all these cases would be likely be "origin" or whichever remote you might usually reference.
如果您只想要远程上存在的标签,只需删除所有本地标签:
然后获取所有远程标签:
If you only want those tags which exist on the remote, simply delete all your local tags:
And then fetch all the remote tags:
自 v1.7.8 起,所有版本的 Git 都可以使用 refspec 来理解
git fetch
,而自 v1.9.0 起,--tags
选项会覆盖--prune
> 选项。对于通用解决方案,请尝试以下操作:要进一步阅读 Git v1.9.0 中“--tags”和“--prune”行为如何更改,请参阅:https://github.com/git/git/commit/e66ef7ae6f31f246dead62f574cc2acb75fd001c
All versions of Git since v1.7.8 understand
git fetch
with a refspec, whereas since v1.9.0 the--tags
option overrides the--prune
option. For a general purpose solution, try this:For further reading on how the "--tags" with "--prune" behavior changed in Git v1.9.0, see: https://github.com/git/git/commit/e66ef7ae6f31f246dead62f574cc2acb75fd001c
好问题。 :) 我没有完整的答案...
也就是说,您可以通过 git ls-remote 获取远程标签列表。要列出
origin
引用的存储库中的标签,您可以运行:这会返回哈希值和友好标签名称的列表,例如:
您当然可以编写一个 bash 脚本来比较由此列表包含您本地拥有的标签。看一下 git show-ref --tags,它生成的标签名称与 git ls-remote 的形式相同。
顺便说一句, git show-ref 有一个选项与您想要的相反。以下命令将列出远程分支上您本地没有的所有标签:
Good question. :) I don't have a complete answer...
That said, you can get a list of remote tags via
git ls-remote
. To list the tags in the repository referenced byorigin
, you'd run:That returns a list of hashes and friendly tag names, like:
You could certainly put together a bash script to compare the tags generated by this list with the tags you have locally. Take a look at
git show-ref --tags
, which generates the tag names in the same form asgit ls-remote
).As an aside,
git show-ref
has an option that does the opposite of what you'd like. The following command would list all the tags on the remote branch that you don't have locally:在新的 git 版本(如 v2.26.2 或更高版本)中,您可以使用
--prune-tags
所以你需要运行:
In new git versions (like v2.26.2 or higher) you could use
--prune-tags
So you would need to run:
Git 原生支持本地标签的清理
该命令拉入最新的标签并删除所有已删除的标签
Git natively supports cleanup of local tags
This command pulls in the latest tags and removes all deleted tags
我知道我参加聚会迟到了,但现在有一个快速答案:
是的,现在是一个获取选项。
如果您不想获取,只需修剪:
I know I'm late to the party, but now there's a quick answer to this:
Yes, it's now an option to fetch.
If you don't want to fetch, and just prune:
这是一个好方法:
git tag -l | xargs git tag -d && git fetch -t
来源:demisx.GitHub.io
this is a good method:
git tag -l | xargs git tag -d && git fetch -t
Source: demisx.GitHub.io
与 @Richard W 的答案相同,但适用于 Windows (PowerShell)
The same answer as @Richard W but for Windows (PowerShell)
更新@2021/05
将
$REPO
参数传递给自定义脚本。sync_git_tags.sh
的内容旧
我将该命令作为 MacOS 上的自定义操作添加到
SourceTree
中。通过
Sourcetree
设置自定义操作
->首选项...
->自定义操作
`Script to run` have to be the `git` path.
我使用
git fetch --prune --prune-tags origin
将标签从远程同步到本地。Updated @2021/05
Pass
$REPO
parameter to custom script.The content of
sync_git_tags.sh
Old
I add the command to
SourceTree
as a Custom Action on my MacOS.Setting
Custom Actions
bySourcetree
->Preferences...
->Custom Actions
`Script to run` have to be the `git` path.
I use
git fetch --prune --prune-tags origin
to sync tags from remote to local.显示本地标签和远程标签之间的区别:
git tag
给出本地标签的列表git ls-remote --tags
给出远程标签的完整路径列表切-f2 | grep -v '\^' | grep -v '\^' | sed 's#refs/tags/##'
从远程标记路径列表中解析出标记名称以“<”开头的行是您的本地标记不再在远程仓库中。如果它们很少,您可以手动将它们一一删除,如果它们很多,您可以执行更多的 grep 和管道来自动化它。
Show the difference between local and remote tags:
git tag
gives the list of local tagsgit ls-remote --tags
gives the list of full paths to remote tagscut -f2 | grep -v '\^' | sed 's#refs/tags/##'
parses out just the tag name from list of remote tag pathsThe lines starting with "< " are your local tags that are no longer in the remote repo. If they are few, you can remove them manually one by one, if they are many, you do more grep-ing and piping to automate it.
刚刚在 GitHub 上的pivotal_git_scripts Gem fork中添加了一个 gitsync-local-tags 命令:
https://github.com/kigster/ git_scripts
安装 gem,然后在存储库中运行“gitsync-local-tags”以删除远程上不存在的本地标签。
或者,您可以安装下面的脚本并将其命名为“git-sync-local-tags”:
Just added a git sync-local-tags command to pivotal_git_scripts Gem fork on GitHub:
https://github.com/kigster/git_scripts
Install the gem, then run "git sync-local-tags" in your repository to delete the local tags that do not exist on the remote.
Alternatively you can just install this script below and call it "git-sync-local-tags":
怎么样 - 删除所有本地标签然后重新获取?
考虑到您的存储库可能包含子模块:
How about this - drop all local tags and then re-fetch?
Considering your repo might contain submodules:
TortoiseGit 现在可以比较标签了。
左侧日志位于远程,右侧日志位于本地。
使用同步对话框的比较标签功能:
另请参阅 TortoiseGit 问题 2973
TortoiseGit can compare tags now.
Left log is on remote, right is at local.
Using the Compare tags feature of Sync dialog:
Also see TortoiseGit issue 2973