git 如何从多个提交中推送多个分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13000563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:48:16  来源:igfitidea点击:

How to push multiple branches from multiple commits?

gitgithub

提问by MiniGod

I never used gitbefore GitHubreleased the Windows app, so I've never used it in command line.

gitGitHub发布 Windows 应用程序之前从未使用过,所以我从未在命令行中使用过它。

So here's my situation:
I did some commits on master, then switched branch and did some commits there too. All without pushing to GitHub. When I then clicked syncin the the windows app (which I assume does git push), to my surprise, all my commits were pushed to my new branch - even the commits I made while I was in master.

所以这是我的情况:
我在 上做了一些提交master,然后切换了分支并在那里也做了一些提交。无需推送到 GitHub。然后sync,当我单击Windows 应用程序(我认为是这样git push)时,令我惊讶的是,我所有的提交都被推送到了我的新分支——即使是我在master.

Since this is the behavior of the windows app, I guess I have to use the command line.
What is the correct git pushcommand to push the commits to the correct branches on the remote?

由于这是 windows 应用程序的行为,我想我必须使用命令行。将提交推送到远程正确分支
的正确git push命令是什么?

回答by kenorb

To push all branches (refs under refs/heads), use the following command (where origin is your remote):

要推送所有分支(refs/heads 下的 refs),请使用以下命令(其中 origin 是您的遥控器):

git push origin --all


You can also set push.defaultto matchingin your config to push all branches having the same name on both ends by default. For example:

您还push.default可以matching在配置中设置为默认推送两端具有相同名称的所有分支。例如:

git config --global push.default matching

Since Git 2.0 the default is simplewhich is the the safest option.

从 Git 2.0 开始,默认值是simple最安全的选项。

回答by Zitrax

If you want to push several specific branches (for example branch1 and branch2) you can use:

如果要推送多个特定分支(例如 branch1 和 branch2),可以使用:

git push origin branch1 branch2 

In Git >= 2.4 this operation can be done atomically (i.e. if it fails to push any of the branches specified nothing will be pushed):

在 Git >= 2.4 中,此操作可以自动完成(即,如果它未能推送指定的任何分支,则不会推送任何内容):

git push --atomic origin branch1 branch2 

回答by Eric

git push originwill push from all tracking branches up to the remote by default.

git push origin默认情况下,将从所有跟踪分支推送到远程。

git push origin my-new-branchwill push just your new branch.

git push origin my-new-branch只会推送你的新分支。

I don't believe there is anything simple or possible to do accidentally that would push commits from two different branches up to the same branch and do the merge on the remote.

我不相信有任何简单或可能会意外地将来自两个不同分支的提交推送到同一个分支并在远程进行合并。

I would guess that the new branch had the commits from master in it's history. To see if that is true, run git log my-new-branchlocally and see if those commits were in your history.

我猜想新分支在它的历史上有来自 master 的提交。要查看这是否属实,请在git log my-new-branch本地运行并查看这些提交是否在您的历史记录中。

If so, when you "switched branches" you probably branched off of master afterthe new commits were made, so the new branch had all of the commits in the history, no just the ones unique to that branch.

如果是这样,当您“切换分支”时,您可能会在进行新提交从 master分支,因此新分支具有历史记录中的所有提交,而不仅仅是该分支独有的提交。

回答by K-Gun

Late answer but I am gonna share my solution that worked for me.

迟到的答案,但我要分享对我有用的解决方案。

Finally my /foo/.git/configfile looks like:

最后我的/foo/.git/config文件看起来像:

[core]
    ...

[remote "dev"]
    url = http://dev.foobar.com/foo.git
    fetch = +refs/heads/*:refs/remotes/dev/*
[remote "pro"]
    url = http://pro.foobar.com/foo.git
    fetch = +refs/heads/*:refs/remotes/pro/*

[remote "all"]
    url = http://dev.foobar.com/foo.git
    url = http://pro.foobar.com/foo.git

[http]
    postBuffer = 524288000

And command;

并指挥;

git push all --all

Credits: Pushing to Multiple Git Repositories Simultaneously

积分:同时推送到多个 Git 存储库

回答by Loic H

You can do a

你可以做一个

 git branch | grep "<pattern>" | xargs git push 

Replace the <pattern>with a regular expression to match your branch names and that's it.

<pattern>正则表达式替换以匹配您的分支名称,就是这样。

You can add --set-upstream originif it is your first commit for this branch

--set-upstream origin如果这是您对该分支的第一次提交,您可以添加