You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

git --sort=v:refname遵循哪种版本排序约定?

Git版本标签排序遵循哪种版本约定?

在版本控制最佳实践中,常会提到SemVer(语义化版本),Python包则普遍使用PEP440版本规范。我想明确:当使用git tag -l --sort=v:refname命令列出标签时,Git遵循的是哪种版本约定?

我对此存在困惑:按照SemVer的规则,1.0.0-2属于「1.0.0预发布版本2」,1.0.0+2属于「1.0.0构建版本2」;但我的测试结果显示,Git会将带-后缀的版本排在+后缀版本及无后缀版本之后,这种排序逻辑更符合PEP440的规则。

测试命令及结果

$ mkdir test-git && cd test-git && git init && touch a && git add a && git commit -m 'initial'
$ for v in 3.6.9 3.6.9-0 3.6.9-1 3.6.9-2 3.6.9+0 3.6.9+1 3.6.10 3.6.10-0 3.6.10-1 3.6.10-2 3.6.10+1 3.6.10+2; do git tag $v; done
$ git tag -l
3.6.10
3.6.10+1
3.6.10+2
3.6.10-0
3.6.10-1
3.6.10-2
3.6.9
3.6.9+0
3.6.9+1
3.6.9-0
3.6.9-1
3.6.9-2

$ git tag -l --sort=v:refname # same output than git tag -l | sort -V
3.6.9
3.6.9+0
3.6.9+1
3.6.9-0
3.6.9-1
3.6.9-2
3.6.10
3.6.10+1
3.6.10+2
3.6.10-0
3.6.10-1
3.6.10-2

背景

我正在管理一个复刻Docker镜像的版本(仅添加不影响应用运行的文件),但Docker不支持+后缀的版本号,原本打算用-后缀实现类似PEP440的post-release版本逻辑,可SemVer中-代表预发布版本,再加上Git的排序行为和PEP440一致,这让我陷入了困惑。


内容的提问来源于stack exchange,提问作者NdFeB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 13:31:17