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

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


当前回答

如果你想创建远程分支而不需要本地分支,你可以这样做:

git push origin HEAD:refs/heads/foo

它将您的HEAD推送到远程上不存在的分支foo。

其他回答

如果新创建的分支不是从同一个repo派生的,即如果您没有使用gitcheckout-b newbranch创建新分支,则gitpush-u<remotename><branch name>不起作用。

例如,我在本地克隆了两个不同的存储库,我必须将repo2/branch1复制到repo1/,然后再将其推送。

此链接帮助我将本地分支(从另一个回购中克隆)推送到远程回购:

如前面的回答中所述,

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

足以推动本地分支。

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

git remote update

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

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

我知道这个问题得到了很好的回答,但我只想列出创建一个新分支“myNewBranch”并推送到远程(在我的情况下是“源”)并设置跟踪所采取的步骤。将其视为“TL;DR”版本:)

# create new branch and checkout that branch
git checkout -b myNewBranch
# now push branch to remote 
git push origin myNewBranch
# set up the new branch to track remote branch from origin
git branch --set-upstream-to=origin/myNewBranch myNewBranch

这里有一个例子,我只有两个分支首先是本地的:起源和移动测试。

直到我在命令行中使用它来实际显示远程分支中更新的文件时,我才开始工作。

git push --set-upstream origin mobile-test

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

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