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

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


当前回答

如前面的回答中所述,

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

足以推动本地分支。

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

git remote update

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

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

其他回答

从现有分支创建本地分支(可以是主分支/开发分支/任何其他分支)。

git checkout-b分支名称

将此推送到远程

git push-u remote_name local_branch_name:remote_branch-name

在这里

-u:设置上游分支remote_name:git在创建存储库时默认将名称设置为“origin”。但是,可以将其更改为其他任意名称。local_branch_name:是要推送的本地分支的名称。remotebranch_name:是要在远程上创建的远程分支的名称。

如果我们删除本地和远程分支名称,它将具有以下格式

git push-u remote_name分支名称

这将将本地分支推送到远程,并使用与本地分支branch_name相同的名称。本地分支也将跟踪远程分支。

[快速回答]

您可以分两步完成:

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

git checkout -b yourBranchName

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

git push -u origin yourBranchName

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

如果要从当前分支创建分支

git checkout -b {your_local_branch_name}

如果您想要远程分支的分支,可以尝试

git checkout -b {your_local_branch_name} origin/<remote_branch_name>

如果完成了更改,则可以添加文件。

git add -A or git add <each_file_names>

然后在本地提交

git commit -m 'your commit message'

当您想推送到远程回购时

git push -u origin <your_local_branch_name>

所有人都将

git checkout -b bug_fixes 

或如果要从远程分支创建本地分支bug_fixes,请说development

git checkout -b bug_fixes origin/development

您可以通过

git push -u origin bug_fixes

任何时候你想从任何其他分支更新你的分支,比如master,

git pull origin master

简单的Git 2.0+解决方案:

从Git2.0开始,行为变得更加简单:

您可以使用push.default=current配置git,以使生活更轻松:

我添加了这个,所以现在我可以用

$ git push -u

-您将跟踪同名的远程分支。现在使用此配置,您将自动猜测gitpush的远程引用。从git.config文档中:

推送故障定义如果没有明确给定refspec,git push应该采取的操作。push.default=current-推送当前分支以使用接收端的名称相同。可在中央和非中央工作流中工作。

对我来说,这是我日常Git工作流程的一个很好的简化。配置设置负责在本地添加分支并希望远程创建分支的“通常”用例。此外,我也可以通过执行git-co-remote_branch_name(而不是使用--set upstream-to-flag),从远程创建本地分支。

我知道这个问题和公认的答案都很陈旧,但行为已经改变,现在有了配置选项,可以简化您的工作流程。

要添加到全局Git配置,请在命令行上运行以下命令:

$ git config --global push.default current

如果已使用--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