我创建了一个本地分支。如何将其推送到远程服务器?
更新:我在这里为Git2.0写了一个更简单的答案。
我创建了一个本地分支。如何将其推送到远程服务器?
更新:我在这里为Git2.0写了一个更简单的答案。
当前回答
首先,创建一个新的本地分支并签出:
git checkout -b <branch-name>
当您将远程分支推送到远程服务器时,将自动创建远程分支:
git push <remote-name> <branch-name>
<remote name>通常是origin,这是git给从中克隆的远程设备的名称。然后,你的同事可以简单地拉那根树枝。
但请注意,正式的格式是:
git push <remote-name> <local-branch-name>:<remote-branch-name>
但如果省略一个,则假定两个分支名称相同。说了这句话,作为一个警告,不要犯只指定:<remote branch name>(带冒号)的严重错误,否则远程分支将被删除!
为了让后续的git pull知道该怎么做,您可能需要使用:
git push --set-upstream <remote-name> <local-branch-name>
如下所述,--set upstream选项设置上游分支:
对于每个最新或成功推送,添加上游(跟踪)引用,由使用无参数git pull(1)和其他命令。
其他回答
我知道这个问题得到了很好的回答,但我只想列出创建一个新分支“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,当您在正确的分支中时,您只需键入
git push—设置上游起点<远程分支名称>
git为您创建原始分支。
[快速回答]
您可以分两步完成:
1.使用签出创建本地分支:
git checkout -b yourBranchName
2.使用push命令自动创建分支并将代码发送到远程存储库:
git push -u origin yourBranchName
有多种方法可以做到这一点,但我认为这种方法非常简单。
如何通过源树进行操作
1: Open SourceTree, click on Repository -> Checkout
2: Click on Create New Branch
3: Select the branch where you want to get code for new branch
4: Give your branch name
5: Push the branch (by click on Push-button)
以下是你如何通过Egit在日食中做到这一点。
转到“Git Repository Exploring”视图,展开要创建分支的Git项目。在分支->本地..下。。选择要为其创建分支的分支(在我的例子中,我选择了master。如果您愿意,可以选择另一个分支)。。然后右键单击并单击“创建分支”选项。。并选择签出此项目选项,然后单击完成按钮。现在从项目浏览器中选择项目。。右键单击“团队”->“推送分支”。
将创建一个新的远程分支。你可以把分支的名称告诉你的同事,这样他们就可以把它取出来。