git 为什么我需要明确推送一个新分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17096311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do I need to explicitly push a new branch?
提问by Cratylus
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?
我是新来的git
,我正在练习。我创建了一个本地分支,但我看到当我创建时,git push
我的分支没有上传到存储库。我必须真正做到:git push -u origin --all
。
为什么是这样?默认情况下,分支不是要推送的新更改吗?为什么我需要运行第二个命令?
回答by VonC
The actual reason is that, in a new repo (git init), there is no branch(no master
, no branch at all, zero branches)
实际原因是,在新的 repo (git init) 中,没有分支(不master
,根本没有分支,零分支)
So when you are pushing for the first time to an emptyupstream repo(generally a bare one), that upstream repo has no branch of the same name.
因此,当您第一次推送到一个空的上游仓库(通常是一个裸仓库)时,该上游仓库没有同名的分支。
And:
和:
- 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 branchon upstream, since git 1.7.11)
- 在默认推送策略是“
matching
”(推同名的所有分支,创建它们,如果它们不存在), - 默认推送策略现在是 '
simple
'(仅推送当前分支,并且仅当它在上游具有类似命名的远程跟踪分支时,因为 git 1.7.11)
In both cases, since the upstream empty repo has no branch:
在这两种情况下,由于上游空仓库没有分支:
- there is no matching named branch yet
- there is no upstream branch at all (with or without the same name! Tracking or not)
- 还没有匹配的命名分支
- 根本没有上游分支(有或没有同名!跟踪与否)
That means your local firstpush has no idea:
这意味着您本地的第一次推送不知道:
- where to push
- what to push (since it cannot find any upstream branch being either recorded as a remote tracking branch, and/or having the same name)
- 推到哪里
- 要推送什么(因为它找不到任何被记录为远程跟踪分支和/或具有相同名称的上游分支)
So you need at least to do a:
所以你至少需要做一个:
git push origin master
But if you do only that, you:
但如果你只这样做,你:
- will create an upstream
master
branch on the upstream (now non-empty repo): good. - won't recordthat the local branch '
master
' needs to be pushed to upstream (origin
) 'master
' (upstream branch): bad.
- 将在上游创建一个上游
master
分支(现在非空回购):好。 - 不会记录本地分支 '
master
' 需要推送到上游 (origin
) 'master
'(上游分支):坏。
That is why it is recommended, for the first push, to do a:
这就是为什么建议在第一次推送时执行以下操作:
git push -u 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
.
这将记录origin/master
为远程跟踪分支,并使下一次推送能够自动推master
送到origin/master
.
git checkout master
git push
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.
这也适用于推送策略“ current
”或“ upstream
”。
在每种情况下,在初始之后git push -u origin master
,一个简单的 git push 就足以继续将 master 推送到正确的上游分支。
回答by John Culviner
You don't, see below
你没有,见下文
I find this 'feature' rather annoying since I'm not trying to launch rockets to the moon, just push my damn branch. You probably do too or else you wouldn't be here!
我觉得这个“功能”很烦人,因为我不是想向月球发射火箭,只是推动我该死的分支。你可能也会这样做,否则你就不会在这里!
Here is the fix: if you want it to implicitly push for the current branch regardless of if that branch exists on origin just issue this command once and you will neverhave to again anywhere:
这是修复:如果您希望它隐式推送当前分支,而不管该分支是否存在于原点,只需发出此命令一次,您将永远不必再在任何地方:
git config --global push.default current
So if you make branches like this:
所以如果你做这样的分支:
git checkout -b my-new-branch
and then make some commits and then do a
然后做一些提交,然后做一个
git push -u
to get them out to origin (being on that branch) and it will create said branch for you if it doesn't exist.
把它们弄到原点(在那个分支上),如果它不存在,它会为你创建所述分支。
Note the -u bit makes sure they are linked if you were to pull later on from said branch. If you have no plans to pull the branch later (or are okay with another one liner if you do) -u is not necessary.
请注意 -u 位确保如果您稍后从所述分支中拉出它们是链接的。如果您不打算稍后拉分支(或者如果您这样做也可以使用另一条衬里)-u 没有必要。
回答by Klas Mellbourn
Output of git push
when pushing a new branch
git push
推送新分支时的输出
> git checkout -b new_branch
Switched to a new branch 'new_branch'
> git push
fatal: The current branch new_branch has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin new_branch
A simple git push
assumes that there already exists a remote branch that the current local branch is tracking. If no such remote branch exists, and you want to create it, you must specify that using the -u
(short form of --set-upstream
) flag.
一个简单的git push
假设已经存在当前本地分支正在跟踪的远程分支。如果不存在这样的远程分支,并且您想要创建它,则必须使用-u
( 的缩写形式--set-upstream
)标志指定。
Why this is so? I guess the implementers felt that creating a branch on the remote is such a major action that it should be hard to do it by mistake. git push
is something you do all the time.
为什么会这样?我猜实现者认为在遥控器上创建分支是一项重大操作,应该很难错误地做到这一点。git push
是你一直在做的事情。
"Isn't a branch a new change to be pushed by default?" I would say that "a change" in Git is a commit. A branch is a pointer to a commit. To me it makes more sense to think of a push as something that pushes commits over to the other repositories. Which commits are pushed is determined by what branch you are on and the tracking relationship of that branch to branches on the remote.
“分支不是默认要推送的新更改吗?” 我会说 Git 中的“更改”是一次提交。分支是指向提交的指针。对我来说,将推送视为将提交推送到其他存储库的事情更有意义。推送哪些提交取决于您所在的分支以及该分支与远程分支的跟踪关系。
You can read more about tracking branches in the Remote Branches chapter of the Pro Git book.
您可以在 Pro Git 书籍的远程分支一章中阅读有关跟踪分支的更多信息。
回答by Fred Foo
I couldn't find a rationale by the original developers this quickly, but I can give you an educated guess based on a few years of Git experience.
我无法这么快地找到原始开发人员的基本原理,但我可以根据几年的 Git 经验给你一个有根据的猜测。
No, not every branch is something you want to push to the outside world. It might represent a private experiment.
不,并非每个分支都是您想要推向外界的东西。它可能代表一个私人实验。
Moreover, where should git push
send all the branches? Git can work with multiple remotes and you may want to have different sets of branches on each. E.g. a central project GitHub repo may have release branches; a GitHub fork may have topic branches for review; and a local Git server may have branches containing local configuration. If git push
would push all branches to the remote that the current branch tracks, this kind of scheme would be easy to screw up.
而且,git push
所有的分支应该发送到哪里?Git 可以使用多个遥控器,您可能希望在每个遥控器上都有不同的分支集。例如,一个中央项目 GitHub 存储库可能有发布分支;GitHub fork 可能有主题分支供;并且本地 Git 服务器可能有包含本地配置的分支。如果git push
将所有分支推送到当前分支跟踪的远程,这种方案很容易搞砸。
回答by Aurimas Rek?tys
HEAD is short for current branch so git push -u origin HEAD works. Now to avoid this typing everytime I use alias:
HEAD 是 current branch 的缩写,所以 git push -u origin HEAD 可以工作。现在为了避免每次使用别名时打字:
git config --global alias.pp 'push -u origin HEAD'
git config --global alias.pp 'push -u origin HEAD'
After this, everytime I want to push branch created via git -b branch I can push it using:
在此之后,每次我想推送通过 git -b branch 创建的分支时,我都可以使用:
git pp
混帐
Hope this saves time for someone!
希望这可以为某人节省时间!
回答by Md.Milton
At first check
首先检查
Step-1: git remote -v
//if found git initialize then remove or skip step-2
第 1 步:git remote -v
//如果找到 git initialize 然后删除或跳过第 2 步
Step-2: git remote rm origin
//Then configure your email address globally git
Step-2: git remote rm origin
//然后全局配置你的邮箱地址 git
Step-3: git config --global user.email "[email protected]"
第 3 步: git config --global user.email "[email protected]"
Step-4: git initial
第四步: git initial
Step-5: git commit -m "Initial Project"
//If already add project repo then skip step-6
Step-5: git commit -m "Initial Project"
//如果已经添加了项目仓库则跳过步骤6
Step-6: git remote add origin %repo link from bitbucket.org%
第 6 步: git remote add origin %repo link from bitbucket.org%
Step-7: git push -u origin master
第 7 步: git push -u origin master
回答by Mark Birbeck
I just experienced a further permutation of this issue.
我刚刚经历了这个问题的进一步排列。
I had a branch named feat/XYZ-1234-some-description
because I was working on Jira issue 1234. During the work I created a new Jira issue to track a smaller piece of work, and when I came to push I decided to push to a branch name with this new issue number in:
我有一个分支,feat/XYZ-1234-some-description
因为我正在处理 Jira 问题 1234。在工作期间,我创建了一个新的 Jira 问题来跟踪较小的工作,当我开始推送时,我决定使用这个新的问题编号推送到一个分支名称在:
git push -u origin feat/XYZ-5678-a-different-description # failed
This gave me the error being discussed in this SO thread. But since I was trying to push to a differentbranch name from my current branch, my problem was different to the one described here. I ended up renaming my local branch before I could push it:
这给了我在这个 SO 线程中讨论的错误。但是由于我试图从当前分支推送到不同的分支名称,因此我的问题与此处描述的问题不同。在推送它之前,我最终重命名了我的本地分支:
git branch -m feat/XYZ-1234-some-description feat/XYZ-5678-a-different-description
git push -u origin feat/XYZ-5678-a-different-description # now works
After a bit more reading around I realised that I could have set a src
on the
git push
, either to the current branch name, or just HEAD
if appropriate:
经过多一点读书周围,我意识到,我可以设置一个src
上
git push
,无论是当前分支的名称,或只是HEAD
如果合适的话:
git push -u origin feat/XYZ-1234-some-description:feat/XYZ-5678-a-different-description # also works
回答by Mukesh Kumar
If you enable to push new changes from your new branch first time. And getting below error:
如果您第一次启用从新分支推送新更改。并得到以下错误:
*git push -f
fatal: The current branch Coding_Preparation has no upstream branch.
To push the current branch and set the remote as upstream, use
要推送当前分支并将远程设置为上游,请使用
git push -u origin new_branch_name
** Successful Result:**
git push -u origin Coding_Preparation
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 599 bytes | 599.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'Coding_Preparation' on GitHub by visiting: ...
* [new branch] Coding_Preparation -> Coding_Preparation
Branch 'Coding_Preparation' set up to track remote branch 'Coding_Preparation' from 'origin'.