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

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

问题描述

我是 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.

还有:

在这两种情况下,由于上游空仓库没有分支:

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 自动推送masterorigin/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屋!

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