如何从一个本地分支推送到 github 内的所有远程分支?

How to push from one local branch to all remote branch in github?

我的 git 存储库中有 4 个以上的远程分支。 我的目标是使用 git 命令将一个代码推送到所有这些分支。

所以我想要如下:

git add .
git commit -m "Do all with one command"
git push origin -all

当然,上面的命令可能无法正常工作。

我可以只用上面的 3 个命令来完成这种操作吗?

已经有一个git push --all(注意双连字符),但它可能,或者至少可能不是您想要的。这里真正的问题是你没有正确定义你想要的——或者至少,不完全确定你想要什么想在这里实现。对我来说,特别令人困惑的部分是:

git add .
git commit -m "Do all with one command"

当前分支 上仅一个 新提交。如果您然后使用 git push 发送这个新的提交,但想将它发送到“四个分支”,那可能不会做您想要的。

你用过吗,说:

for branch in br1 br2 br3 br4; do
    git checkout $branch
    [do some editing and/or cherry-picking and so on here]
    git commit
done

这样您就可以更新名为 br1br2br3br4 的本地分支,现在您想要发送 four(或更多)提交到 origin 上的四个相应分支,now 这一切都有意义。

就此而言,这里:

git push --all origin

可能会完全按照您的意愿行事。但是,它可能比您想要的 多,因为它的含义与:

git push origin "refs/heads/*:refs/heads/*"

即,使用 all 您的本地分支名称将请求发送到另一个 Git 相同名称。如果您也有分支 br5br6,那将产生在这些分支上发送任何新提交的效果,并要求 origin 更新其拼写相同的分支名称你的。

如果这确实是您想要做的,请考虑使用 --all 或上面 git push 中的显式 refspec。您还可以将 push.default 设置更改为 matching。大多数用户可能 不应该 使用 matching 设置。这是 Git 1 中的默认值,并在 Git 2 中更改为 simple 因为这对大多数用户来说是错误的,但没有人是“大多数用户”。

如果你的远程是origin,你在master分支,远程分支是firstsecondthirdfourth:

git add .
git commit -m "Do all with one command"
git push origin master:first master:second master:third master:fourth