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

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


当前回答

我用这个,它很方便:

git config --global alias.mkdir '!git checkout -b $1; git status; git push -u origin $1; exit;'

用法:git mkdir NEW_BRANCH

你甚至不需要git状态;也许,我只是想确保一切顺利。。。

您可以通过一个命令同时拥有LOCAL和REMOTE分支。

其他回答

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

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

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

基于当前分支在本地创建新分支:

git checkout -b newbranch

按正常方式提交任何更改。然后,将其向上推:

git push -u origin HEAD

这是一个快捷方式,可以将当前分支推送到原点上同名的分支并跟踪它,这样以后就不需要指定原点HEAD了。

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

然后远程创建分支:

git push --set-upstream origin your_branch

注意:这适用于git的最新版本:

$ git --version
git version 2.3.0

干杯

最简单的解决方案。。。鼓卷。。。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

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