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

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


当前回答

如何通过源树进行操作

 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)

其他回答

最简单的解决方案。。。鼓卷。。。git版本2.10.1(Apple git-78)

1) git checkout -b localBranchNameThatDoesNotExistInRemote

2) Do your changes, and do a git commit 

3) git push origin localBranchNameThatDoesNotExistInRemote --force

注意:您刚刚在本地环境中创建的分支和您尝试推送的远程非现有分支必须具有相同的名称。

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

我使用了两种方法来创建分支

如果您正在使用TortoiseGit,请执行以下步骤:-

1.使用TortoiseGit创建分支

右键单击您的项目>>TortoiseGit>>创建分支>>编写分支的名称并选择基本分支,然后按ok

2.推动分支

右键单击项目>>TortoiseGit>>推送>>单击确定

3.切换到新分支

右键单击您的项目>>TortoiseGit>>切换/签出>>选择新创建的分支并按ok

如果使用命令提示符,请执行以下步骤:-

1.使用命令提示符创建分支

$git签出-b new_branch_name

2.推动分支

$git推送原点new_branch_name

3.切换到新分支它已经切换为new_branch_name,否则您可以使用

$git签出new_branch_name

在本地计算机上创建分支并在此分支中切换:

$ 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]

此处显示所有信息

其他现有项目

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

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

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