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

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


当前回答

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

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

现在使用git,当您在正确的分支中时,您只需键入

git push—设置上游起点<远程分支名称>

git为您创建原始分支。

我通过将其添加到bash~/.profile中解决了这个问题:

函数gitb(){git checkout-b$1&&git push--设置上游原点$1;}

然后,为了启动一个新的本地+远程分支,我写下:

gitb功能/mynewbranch

这将创建分支并执行第一次推送,不仅是为了设置跟踪(以便以后git pull和git push在没有额外参数的情况下工作),而且实际上是为了确认目标回购中没有这样的分支。

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

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相同的名称。本地分支也将跟踪远程分支。

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