且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

循环所有具有特定名称的git分支

更新时间:2023-11-30 11:28:10

我们积极劝阻不要使用任何Porcelain命令,包括git分支,在脚本中 - 来自 Git Maintainer的博客



您想使用类似于

git for-each-ref - -format ='%(refname:short)'refs / heads / feature /



可以在 for $ in $()构造


I have the following git branches

foo
bar
foobar
feature/foo
feature/bar
feature/buzz

How would you do a for loop over all branches that start with the word 'feature/' ?

When I try the following, it strangely prints out more than just the git branches.

for i in $(git branch --list "feature/*"); do echo $i; done;
stuff.txt
icon.jpg
morestuff.txt
Vagrantfile
feature/foo
feature/bar
feature/buzz

"We actively discourage against use of any Porcelain command, including git branch, in scripts" - from the Git Maintainer's Blog

You want to use something like

git for-each-ref --format='%(refname:short)' refs/heads/feature/

which will work fine inside a for br in $() construct