我创建了一个本地分支。如何将其推送到远程服务器?

更新:我在这里为Git2.0写了一个更简单的答案。


当前回答

从Git2.37.0开始,您不再需要“--setupstreamorigin”。只要按一下。您可以通过启用push.autoSetupRemote选项来实现这一点

git-config--global--add--bool push.autoSetupRemote true

其他回答

如前面的回答中所述,

git push <remote-name> <local-branch-name>:<remote-branch-name>

足以推动本地分支。

您的同事可以使用以下命令拉动所有远程分支(包括新分支):

git remote update

然后,要对分支进行更改,通常的流程如下:

git checkout -b <local-branch-name> <remote-name>/<remote-branch-name>

[快速回答]

您可以分两步完成:

1.使用签出创建本地分支:

git checkout -b yourBranchName

2.使用push命令自动创建分支并将代码发送到远程存储库:

git push -u origin yourBranchName

有多种方法可以做到这一点,但我认为这种方法非常简单。

从Git2.37.0开始,您不再需要“--setupstreamorigin”。只要按一下。您可以通过启用push.autoSetupRemote选项来实现这一点

git-config--global--add--bool push.autoSetupRemote true

如果已使用--single branch克隆当前分支,请使用此命令从当前分支创建新分支:

git checkout -b <new-branch-name>
git push -u origin <new-branch-name>
git remote set-branches origin --add <new-branch-name>
git fetch

基于当前分支在本地创建新分支:

git checkout -b newbranch

按正常方式提交任何更改。然后,将其向上推:

git push -u origin HEAD

这是一个快捷方式,可以将当前分支推送到原点上同名的分支并跟踪它,这样以后就不需要指定原点HEAD了。