什么是上游 git

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17122245/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:38:46  来源:igfitidea点击:

What is a git upstream

git

提问by patriques

When you have created a github-repo and added the the github-repo as remote

当您创建了 github-repo 并将 github-repo 添加为远程时

git remote add origin https://github.com/githubname/reponame.git

then you need to push your first commit with

那么你需要推送你的第一次提交

git push -u origin master

I read (Why do I need to do `--set-upstream` all the time?) that this a short form for doing

我读到(为什么我需要一直做`--set-upstream`?)这是一个简短的形式

git branch --set-upstream-to my_branch origin/my_branch
git push

What is a upstream exactly and why do I need to set it? There is little information about this on the net. I know that there is a similar topic What does 'git remote add upstream' help achieve?, but in my opinion it does not explain exactly what the upstream is and what git push -u origin masterdoes, especially what is origin masterpointing to, is it the local repo or the remote repo?

什么是上游,为什么我需要设置它?网络上关于这方面的信息很少。我知道有一个类似的主题“git remote add upstream”有助于实现什么?,但在我看来,它并没有准确解释上游是什么以及什么git push -u origin master,特别是origin master指向的是本地回购还是远程回购?

回答by Klas Mellbourn

In the command

在命令中

git push -u origin master

The -uflag means that your local branch will become a tracking branch. That is, a branch that tracks a remote branch (the "upstream" branch), so that future git pullwill know which branch to merge from and git pushwill be directed to the correct remote branch.

-u标志意味着您的本地分支将成为跟踪分支。也就是说,一个跟踪远程分支(“上游”分支)的分支,以便将来git pull知道要从哪个分支合并git push并将被定向到正确的远程分支。

originis the remote repository you are pushing to.

origin是您要推送到的远程存储库。

masteris the refspec parameter. The refspec parameter specifies which local branch is pushed to what remote branch. It can be complicated, but in this case the short form mastermeans to push the local masterbranch to the remote branch with the same name, origin/master.

master是 refspec 参数。refspec 参数指定将哪个本地分支推送到哪个远程分支。它可能很复杂,但在这种情况下,简短形式master意味着将本地master分支推送到具有相同名称的远程分支,origin/master.

Technically, the tracking adds the following information about the masterbranch to your .git/config:

从技术上讲,跟踪会将有关master分支的以下信息添加到您的.git/config:

[branch "master"]
    remote = origin
    merge = refs/heads/master

and it creates a file here .git/refs/remotes/origin/master, representing the remote branch.

并在此处创建一个文件.git/refs/remotes/origin/master,代表远程分支。

回答by Ignacio Vazquez-Abrams

"Upstream" is the repo you cloned (some of) the branches in yours from, and where you push changes to those branches (and optionally entire new branches) once they've been committed. GitHub acts as your upstream because they store the revisions for you, in a centralized location.

“上游”是您从中克隆(部分)分支的存储库,以及在提交后将更改推送到这些分支(以及可选的整个新分支)的位置。GitHub 充当您的上游,因为它们在一个集中位置为您存储修订。