如何使用bash脚本通过所有的git分支迭代 [英] How to iterate through all git branches using bash script

查看:196
本文介绍了如何使用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分支)给我的资料库文件夹内的文件夹在资源库中​​的分支present一起。

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分支的编写脚本时。 Git提供一个<一个href=\"http://www.kernel.org/pub/software/scm/git/docs/git.html#_low_level_commands_plumbing\">plumbing接口被明确地设计用于脚本中使用。

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

你想要的管道命令的混帐的for-each-REF 的:

The plumbing command you want is git for-each-ref:

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


注意:您不需要在遥控器/ preFIX遥控器上的裁判,除非你有这导致其他裁判产地/主来匹配裁判的名字搜索路径多个地方(见的符号参考的名字......在的的git-REV-解析的指定修订部分(1))。如果你想显式地避免歧义,然后用全名裁判去:参/遥控器/产地/主

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

您可以管这个输出到的 SH

You can pipe this output into sh.

如果你不喜欢生成壳code的想法,你可能会放弃一点健壮性 * ,并做到这一点:

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

*
编号名称应该是从外壳的分词安全(见<一href=\"http://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html\">git-check-ref-format(1)).个人而言,我会与前一版本(生成壳$ C $三)坚持;我更相信,没有什么不合适的可以用这一点。

* 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

如果您不使用支持数组壳你就可以用 $ @ 类似的东西(设置 - 初始化和设置 - $ @%(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天全站免登陆