How to iterate through all git branches using bash script
How can I iterate through all the local branches in my repository using bash script. I need to iterate and check is there any difference between the branch and some remote branches. Ex
for branch in $(git branch);
do
git log --oneline $branch ^remotes/origin/master;
done
I need to do something like given above, but the issue I'm facing is $(git branch) gives me the folders inside the repository folder along with the branches present in the repository.
Is this the correct way to solve this issue? Or is there another way to do it?
Thank you
You should not use git branch when writing scripts. Git provides a “plumbing” interface that is explicitly designed for use in scripting (many current and historical implementations of normal Git commands (add, checkout, merge, etc.) use this same interface).
The plumbing command you want is git for-each-ref:
git for-each-ref --shell
--format='git log --oneline %(refname) ^origin/master'
refs/heads/
Note: You do not need the remotes/
prefix on the remote ref unless you have other refs that cause origin/master
to match multiple places in the ref name search path (see “A symbolic ref name. …” in the Specifying Revisions section of git-rev-parse(1)). If you are trying to explictly avoid ambiguity, then go with the full ref name: refs/remotes/origin/master
.
You will get output like this:
git log --oneline 'refs/heads/master' ^origin/master
git log --oneline 'refs/heads/other' ^origin/master
git log --oneline 'refs/heads/pu' ^origin/master
You can pipe this output into sh.
If you do not like the idea of generating the shell code, you could give up a bit of robustness* and do this:
for branch in $(git for-each-ref --format='%(refname)' refs/heads/); do
git log --oneline "$branch" ^origin/master
done
* Ref names should be safe from the shell's word splitting (see git-check-ref-format(1)). Personally I would stick with the former version (generated shell code); I am more confident that nothing inappropriate can happen with it.
Since you specified bash and it supports arrays, you could maintain safety and still avoid generating the guts of your loop:
branches=()
eval "$(git for-each-ref --shell --format='branches+=(%(refname))' refs/heads/)"
for branch in "${branches[@]}"; do
# …
done
You could do something similar with $@
if you are not using a shell that supports arrays ( set --
to initialize and set -- "$@" %(refname)
to add elements).
This is because git branch
marks the current branch with an asterisk, eg:
$ git branch
* master
mybranch
$
so $(git branch)
expands to eg * master mybranch
, and then the *
expands to the list of files in the current directory.
I don't see an obvious option for not printing the asterisk in the first place; but you could chop it off:
$(git branch | cut -c 3-)
如果您的本地分支仅以数字,az和/或AZ字母命名,我会建议使用$(git branch|grep -o "[0-9A-Za-z]+")
上一篇: 主人和分支之间的信息前/后信息?
下一篇: 如何使用bash脚本遍历所有git分支