[英]How to get a list with all branches and the latest tag that includes a number in Git?
我有許多包含不同版本標簽的Git分支。 我正在尋找一種解決方案,其中逐行列出了所有分支機構及其最新編號的標簽。 (版本始終具有相同的格式:XYZ)
例:
碩士:2.2.2
分支B:2.2.1
分支C:2.0.0
也許正是您想要的-
git branch -a | while read branch; do
echo "$branch"; git describe --tags --abbrev=0 $branch
done
它顯示all local & remote branches with latest tag
。
樣本輸出:
* master
0.3
branch-1
0.2.1
remotes/origin/master
0.3
如果您沒有任何多余的標簽,那么@sajibkhan的想法是正確的,
git for-each-ref refs/heads refs/remotes --format='
printf '%s: ' %(refname:short); git describe --tags --abbrev=0 %(refname:short)
' |sh
或者while read
更加熟悉,
git branch -a|sed s/.//|while read branch; do
printf '%s: ' "$branch"; git describe --tags --abbrev=0 $branch
done
但是你的
他們最新的編號標簽
讓我覺得您有一些未編號的標簽,而這些標簽會使git describe
有時給出錯誤的結果。
git describe
具有--match
選項,因此,如果您可以為要考慮的標簽制作一個正匹配的glob,例如,如果--match=[0-9]*
將做到,只需添加。 但是,例如linux具有v4.8.1
和v2.6.3-rc7
,並且僅將第一個標記與glob模式匹配並不容易。
對於這種情況,有一個非常好的ref手術技術:
scratch=`mktemp -d`
git clone -s --mirror . "$scratch"
cd !$
制作一個非常快速的從頭開始克隆,帶有重復的引用,但沒有實際內容。 -s
是--shared
縮寫,存儲庫使用主對象db,鏡像克隆始終是裸露的,因此沒有簽出。 將共享對象克隆用於任何您不確定要得到結果還是前后需要的臨時工作。
因此,既然您位於沙盒鏡中,就可以刪除不想在此處看到的引用:
git for-each-ref refs/tags/*[^0-9.]* --format='
delete %(refname)
' | git update-ref --stdin
刪除任何名稱中任何具有非數字或點的標記。
清理后的便箋本refname空間已准備好用於上面簡單的git describe
。
這是一種通用技術:您可以在任何具有url或路徑名的位置,通過任何所需的重寫來推送和獲取所需的任何引用。 這包括您的本地存儲庫。 例如,這是為每個遠程跟蹤分支建立或快速轉發本地分支的一種殘酷方法:
git fetch . refs/remotes/origin/*:refs/heads/*
(首先在臨時克隆中執行此操作...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.