为什么我需要明确推送新分支? [英] Why do I need to explicitly push a new branch?

查看:342
本文介绍了为什么我需要明确推送新分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 git 中的新成员,并且正在练习。我创建了一个本地分支,但是当我执行 git push 时,我看到我的分支未上传到存储库。我必须真的这样做: git push -u origin --all

为什么会这样?是不是一个分支是默认推送的新变化?为什么我需要运行第二个命令?

解决方案

实际的原因是,在新的repo(git init)中, 不是分支(no master ,根本不分支,零分支)

因此,当您第一次推送到 上游回购时(通常为<上游回购有一个),上游回购





在这两种情况下,由于上游空回购没有分支: p>


  • 尚无匹配的命名分支

  • 根本没有上游分支(有或没有一样的名字!跟踪与否)



这意味着您本地的第一个推送不知道:




  • 在哪里推送

  • 要推送什么(因为它找不到任何上游分支被记录为远程跟踪分支,和/或具有相同的名称)



所以你至少需要做一个:

  git push origin master 

但是如果你只有这样,你:


  • 将在上游创建一个上游 master 分支(现在是非空的回购):很好。

  • 不会记录本地分支 master '需要推送到上游( origin )' master '(上游分支):bad。



这就是为什么建议在第一次推送时执行以下操作:

  git push -u origin master 

这会记录起源/主作为远程跟踪分支,并将启用下一次推送以自动推送 master code>到 origin / master

  git checkout master 
git push

这也适用于推送策略'当前'或'上游'。

在每种情况下,在初始 git push - ü原始大师,一个简单的git push将足以继续推动master到右上游分支。


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?

解决方案

The actual reason is that, in a new repo (git init), there is no branch (no master, no branch at all, zero branches)

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.

And:

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 first push 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 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

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆