为什么我需要明确推送一个新分支? [英] Why do I need to explicitly push a new branch?
问题描述
我是 git
的新手,我正在练习.我创建了一个本地分支,但我看到当我执行 git push
时,我的分支没有上传到存储库.我实际上必须这样做:git push -u origin --all
.
为什么是这样?默认情况下,分支不是要推送的新更改吗?为什么我需要运行第二个命令?
I am new in git
and I am practicing. I created a local branch but I saw that when I did git push
my branch was not uploaded to the repository. I had to actually do: git push -u origin --all
.
Why is this? Isn't a branch a new change to be pushed by default? Why do I need to run the second command?
推荐答案
真正的原因是,在一个新的 repo (git init) 中,没有分支(没有 master
,根本没有分支,零分支)
The actual reason is that, in a new repo (git init), there is no branch (no master
, no branch at all, zero branches)
因此,当您第一次推送到空上游存储库(通常一个 裸机),上游repo 没有同名分支.
So when you are pushing for the first time to an empty upstream repo (generally a bare one), that upstream repo has no branch of the same name.
还有:
- 默认推送策略是
matching
"(推送相同的所有分支名称,如果它们不存在则创建它们), - 默认推送策略现在是
simple
"(仅推送当前分支,并且仅当它在上游具有类似名称的远程跟踪分支时,自 git 1.7.11 起)
在这两种情况下,由于上游空仓库没有分支:
In both cases, since the upstream empty repo has no branch:
- 尚无匹配的命名分支
- 根本没有上游分支(有或没有同名!跟踪与否)
这意味着您本地的第一次推送不知道:
That means your local first push has no idea:
- 推到哪里
- 推送什么(因为它找不到任何被记录为远程跟踪分支和/或具有相同名称的上游分支)
所以你至少需要做一个:
So you need at least to do a:
git push origin master
但如果你只这样做,你:
But if you do only that, you:
- 将在上游创建一个上游
master
分支(现在非空 repo):好. - 不会记录本地分支'
master
'需要推送到上游(origin
)'master
'(上游分支):不好.
- will create an upstream
master
branch on the upstream (now non-empty repo): good. - won't record that the local branch '
master
' needs to be pushed to upstream (origin
) 'master
' (upstream branch): bad.
这就是为什么建议在第一次推送时执行以下操作:
That is why it is recommended, for the first push, to do a:
git push -u origin master
这会将 origin/master
记录为远程跟踪分支,并将启用next push 自动推送master
到origin/master
.
That will record origin/master
as a remote tracking branch, and will enable the next push to automatically push master
to origin/master
.
git checkout master
git push
这也适用于推送策略current
"或upstream
".
在每种情况下,在初始 git push -u origin master
之后,一个简单的 git push 就足以继续将 master 推送到正确的上游分支.
And that will work too with push policies 'current
' or 'upstream
'.
In each case, after the initial git push -u origin master
, a simple git push will be enough to continue pushing master to the right upstream branch.
这篇关于为什么我需要明确推送一个新分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!