用 git describe 生成版本字符串

生成形如 v1.2-5-gabc123 的构建版本信息。

macOSGit命令行版本低风险
运行 git describe --tags --always --dirty,打印最近标签及提交距离。

01命令

命令
$ git describe --tags --always --dirty

02作用说明

describe 把提交变成可排序、可读的标识,能看出距上次发布多远以及工作区是否脏。它常作为构建脚本和 --version 输出的输入,正是终端管道擅长的元数据。

03风险

风险低风险
使用条件Git 2.20+ (install with Homebrew or Xcode Command Line Tools)
系统兼容性macOS with Git installed (Homebrew or Xcode Command Line Tools)
!注意: 没有标签的仓库会退化为短 SHA;请规范打标签以获得有意义的输出。

04撤销

✓无需撤销——该命令不会改变持久状态。

05来源

  • git-describe documentation
    Git · vendor-doc · 2026-09-20
    依据: Documents git describe, which names a commit from the nearest tag.

最后验证: 2026-10-13 · 分类: 开发者

06相关方案