循环所有git分支并应用提交



我的回购中有许多分支。我想循环思考每个分支,签出分支并运行一个命令来更新所有包并提交更改。最后,我想全力以赴。

所以我想办法循环思考分支

for BRANCH in `git branch -a | grep remotes/origin/*` ;
do
A="$(cut -d'/' -f3 <<<"$BRANCH")"
echo $A
done 

不确定这是否是最好的方法。然而,我仍然在努力如何gitcheckout分支,然后继续进行自动提交。

有什么想法吗?

更新

基于@bk2204的回答,我又做了两件事:弄清楚如何在另一个文件夹的上下文中运行脚本https://stackoverflow.com/a/10566581/2926340以及如何自动克隆所有远程分支https://stackoverflow.com/a/40318872/2926340

这就是我想到的:

#!/bin/sh
cd my-folder #<-- tight to a specific folder
:
set -e
[ -z "$(git status --porcelain)" ] || { echo "working tree not clean" >&2; false; }
#for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
#git branch --track ${branch#remotes/origin/} $branch #<-- throws an error if was already cloned
#done
for branch in $(git for-each-ref refs/heads/* | cut -d"$(printf 't')" -f2 | cut -b12-)
do
echo $branch
# This will overwrite any changes.
git checkout -f $branch
# Run command here.
rm -rf node_modules
npm i
# run my update thing
git add .
git commit -m "Update packages"
done

它是有效的,但现在有两个问题:

1( 脚本只在特定的文件夹中运行,所以如果我想在其他文件夹中运行它,我总是必须更改文件夹的路径
2(如果我已经拉取了所有分支#git branch --track ${branch#remotes/origin/} $branch会抛出错误。

您将如何解决这些问题,以便能够在任何文件夹上运行脚本,并能够在特定的远程回购已经被克隆的情况下处理这种情况?

git branch不是为编写脚本而设计的,所以您可能想要做一些不同的事情。也许更像这样的东西会有所帮助:

#!/bin/sh
set -e
[ -z "$(git status --porcelain)" ] || { echo "working tree not clean" >&2; false; }
for branch in $(git for-each-ref refs/heads/* | cut -d"$(printf 't')" -f2 | cut -b12-)
do
# This will overwrite any changes.
git checkout -f $branch
# Run command here.
git add .
git commit -m "Update packages by running command"
done

请注意,您可以在cut调用中放置一个文本选项卡,但我想我会让它对剪切和粘贴错误更有抵抗力。

最新更新