2026年11月28日

Git 的版本編號如何製作?

以前 CSV、SVN 等等,都有版本編號可以查看,由版本編號就可以判斷新舊,就可以透過程式做一些處理,但是 Git 都是 sha-1 的 Hash,要怎麼做出數字的編號呢?

Git 的版本編號如何製作?

如果要像 CVS、SVN 一樣,用數字大小來查看、比較新舊版,可以使用這種方式:

$ git rev-list HEAD | wc -l # 用 commit 次數當 version number 12904

另外若要使用 hash 來當版本編號,有長短可以選擇

短 Hash (速度比較)

  • $ git describe --always # real 0m0.097s d5c364b18
  • $ git log --pretty="%h" -n1 HEAD # real 0m0.004s d5c364b18
  • $ git rev-parse --short HEAD # 短hash 建議使用這個,速度最快, real 0m0.003s d5c364b18

長 Hash

  • $ git rev-parse HEAD d5c364b18aa9c7542bb4c55c4e36545d03c1985a

使用 PHP 抓取版本編號範例

 12904
 [short] => v12904.d5c364b18
 [full] => v12904.d5c364b18aa9c7542bb4c55c4e36545d03c1985a)
*/
?>

原文:Tsung