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

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


当前回答

我知道这个问题得到了很好的回答,但我只想列出创建一个新分支“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

其他回答

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

推动github上的分支:

$ git push origin [name_of_your_new_branch]

当你想在你的分支中提交一些事情时,一定要在你的分行中。

您可以看到使用以下方法创建的所有分支:

$ git branch

将显示:

* approval_messages
  master
  master_clean

为分支添加新的远程:

$ git remote add [name_of_your_remote] 

将提交中的更改推送到分支中:

$ git push origin [name_of_your_remote]

更新官方存储库中的原始分支后,更新分支:

$ git fetch [name_of_your_remote]

然后,您需要申请合并更改,如果您的分支是从开发派生的,则需要执行以下操作:

$ git merge [name_of_your_remote]/develop

删除本地文件系统上的分支:

$ git branch -d [name_of_your_new_branch]

要强制删除文件系统上的本地分支,请执行以下操作:

$ git branch -D [name_of_your_new_branch]

删除github上的分支:

$ git push origin :[name_of_your_new_branch]

此处显示所有信息

其他现有项目

首先,创建一个新的本地分支并签出:

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)和其他命令。

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

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 checkout -b {branchName}

创建一个新分支,它也会检出该分支/使其成为当前分支。如果出于某种原因,您只想捕捉一个分支,而不想将其作为当前分支,则可以使用以下命令:

git branch {branchName}

在第一个命令中,“checkout”使所述分支成为当前分支,“-b”表示:该分支还不存在,所以请为我创建。