如何使用 bash 脚本遍历所有 git 分支 [英] How to iterate through all git branches using bash script

查看:40
本文介绍了如何使用 bash 脚本遍历所有 git 分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 bash 脚本遍历存储库中的所有本地分支.我需要迭代并检查分支和一些远程分支之间是否有任何区别.例

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

我需要做上面给出的事情,但我面临的问题是 $(git branch) 给了我存储库文件夹中的文件夹以及存储库中存在的分支.

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?

谢谢

推荐答案

编写脚本时不应使用 git branch.Git 提供了一个管道"接口明确设计用于脚本编写(普通 Git 命令(添加、签出、合并等)的许多当前和历史实现都使用相同的接口).

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).

你想要的管道命令是git for-each-ref:

git for-each-ref --shell 
  --format='git log --oneline %(refname) ^origin/master' 
  refs/heads/

注意:您不需要远程引用上的 remotes/ 前缀,除非您有其他引用导致 origin/master 匹配引用名称搜索路径中的多个位置(请参阅符号引用名称.……"在 git-rev-parse(1) 的指定修订部分).如果您试图明确避免歧义,请使用完整的参考名称:refs/remotes/origin/master.

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.

你会得到这样的输出:

git log --oneline 'refs/heads/master' ^origin/master
git log --oneline 'refs/heads/other' ^origin/master
git log --oneline 'refs/heads/pu' ^origin/master

您可以将此输出通过管道传输到 sh.

You can pipe this output into sh.

如果您不喜欢生成 shell 代码的想法,您可以放弃一些健壮性* 并这样做:

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

*引用名称应该不会受到 shell 的分词(参见 git-check-ref-format(1)).我个人会坚持使用以前的版本(生成的 shell 代码);我更有信心,它不会发生任何不恰当的事情.

* 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.

因为您指定了 bash 并且它支持数组,所以您可以保持安全并避免产生循环的内脏:

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

如果您不使用支持数组的 shell (set -- 初始化和 set -- "$@" %(refname) 添加元素.

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).

这篇关于如何使用 bash 脚本遍历所有 git 分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆