I'm looking for a shell/bash script to do the following Git commands:
I was able to do it using 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. Anyone? My initial guess would be to do something like
for BRANCH in $(git branch -r); (...); done
However, git branch -r
outputs
origin/HEAD -> origin/master
origin/master
origin/some_branch_a
origin/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. However, my bash-scripting skills are quite limited:
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. The following is based on the code, not the description.
Use git for-each-ref
, which finds references within some part of the namespace. The namespace here is refs/remotes/origin
:
git for-each-ref --format='%(refname:short)' refs/remotes/origin |
while read refname; do
...
done
Strip off origin/
:
name=${refname#origin/}
Check for and discard HEAD
and master
since you do not want to operate on them:
case "$name" in
HEAD|master) continue;;
esac
(or the same with if
if you prefer).
You're now ready to use git checkout $name
, git merge master
, and git push
as before. Note that it's more efficient to do one final git push
of every name, but that's a little more complex.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.