[英]Merging master into all origin branches using bash
I'm looking for a shell/bash script to do the following Git commands: 我正在寻找一个shell / bash脚本来执行以下Git命令:
I was able to do it using Python: 我能够使用Python做到这一点:
tmp_folder = "tmp-clone"
if os.path.exists(tmp_folder):
shutil.rmtree(tmp_folder)
g = git.Git()
g.execute(["git", "clone", "...", tmp_folder])
g = git.Git("tmp-clone")
branches = g.execute(["git", "branch", "-r"])
matches = [item for item in re.findall(r"origin/([\w-]+)", branches) if item not in ("HEAD", "master")]
for branch in matches:
print("merge master -> %s" % branch)
g.execute(["git", "checkout", branch])
g.execute(["git", "merge", "master"])
g.execute(["git", "push"])
But I'm looking to do this without Python. 但我希望在没有Python的情况下执行此操作。 Anyone?
任何人? My initial guess would be to do something like
我最初的猜测是做类似的事情
for BRANCH in $(git branch -r); (...); done
However, git branch -r
outputs 但是,
git branch -r
输出
origin/HEAD -> origin/master
原点/头->原点/主
origin/master
起源/主人
origin/some_branch_a
起源/ some_branch_a
origin/some_branch_b
起源/ some_branch_b
and (in this example) I'm only interested in some_branch_a
and some_branch_b
(hence the regex in my Python), so I also need some kind of regex in the bash script. 并且(在此示例中)我只对
some_branch_a
和some_branch_b
(因此在我的Python中使用正则表达式)感兴趣,因此在bash脚本中我还需要某种正则表达式。 However, my bash-scripting skills are quite limited: 但是,我的bash脚本编写技能非常有限:
for BRANCH in $(git branch -r);
if $BRANCH matches "origin/([\w-]+)" ???
git checkout $BRANCH;
git merge origin/master;
git push $BRANCH;
fi
done
Note: Assume that conflicts can NOT occur. 注意:假设不会发生冲突。
Your Python code does not do what your text description suggests. 您的Python代码不执行您的文本描述所建议的。 The following is based on the code, not the description.
以下内容基于代码,而非说明。
Use git for-each-ref
, which finds references within some part of the namespace. 使用
git for-each-ref
,它在命名空间的某些部分中找到引用。 The namespace here is refs/remotes/origin
: 这里的命名空间是
refs/remotes/origin
:
git for-each-ref --format='%(refname:short)' refs/remotes/origin |
while read refname; do
...
done
Strip off origin/
: 去除
origin/
:
name=${refname#origin/}
Check for and discard HEAD
and master
since you do not want to operate on them: 检查并丢弃
HEAD
和master
因为您不想对其进行操作:
case "$name" in
HEAD|master) continue;;
esac
(or the same with if
if you prefer). (或以相同的
if
你愿意)。
You're now ready to use git checkout $name
, git merge master
, and git push
as before. 现在,您可以像以前一样使用
git checkout $name
, git merge master
和git push
了。 Note that it's more efficient to do one final git push
of every name, but that's a little more complex. 请注意,对每个名称进行一次最终的
git push
效率更高,但这有点复杂。
To check whether your script is really ready, before actually doing anything, make it echo the commands it would have done: 要检查您的脚本是否真的准备就绪,请在执行任何操作之前使其回显应执行的命令:
git for-each-ref --format='%(refname:short)' refs/remotes/origin |
while read refname; do
name=${refname#origin/}
case "$name" in
HEAD|master) continue;;
esac
echo git checkout $name
echo git merge master
echo git push $name
done
Note: the above is all untested. 注意:以上均未经测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.