当我在本地commit完成,直接使用git push 出现了如下错误:
The current branch master has no upstream branch
提示目前的branch 没有upstream branch.
git pro 里面关于upstream branch的解释:
Checking out a local branch from a remote-tracking branch automatically creates what is called a
“tracking branch” (and the branch it tracks is called an “upstream branch”). Tracking branches are
local branches that have a direct relationship to a remote branch.
意思是远程的branch和本地的branch可以发生联系,这种联系是一种对应关系,当我们push 的时侯本地的就会替换远程的。发生这种关系的branch一个叫做upstream branch(remote),一个叫做tracking branch(local).
git 的push逻辑是:
- the default push policy was ‘matching’ (push all the branches of the same name, creating them if they don’t exist),
- the default push policy is now ‘simple’ (push only the current branch, and only if it has a similarly named remote tracking branch on upstream, since git 1.7.11)
也就是说本地的branch必须在远程中有upstream branch 才会push成功。
解决办法是让两个branch发生联系:
git push --set-upstream origin master
简写
git push -s origin master
这样操作以后系统回复
branch 'master' set up to track 'origin/master'.
这样下一次push可以直接使用
git push
有一个问题是如果我在本地新建一个new_branch那么git push就不能提交了,原因和前面一样。
必须要重新set upstream branch.
git push --set-upstream origin new_branch
简写
git push -u origin new_branch