git branch --set-upstream-to 与 git remote add origin 之间的区别

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

Difference between git branch --set-upstream-to vs git remote add origin

gitversion-controlversioning

提问by dev02

I find it little confusing to know the difference between git branch --set-upstream-tovs git remote add originor even git remote add upstream

我发现了解git branch --set-upstream-tovsgit remote add origin或 even之间的区别并不令人困惑git remote add upstream

Basically I have a bare repository created with git init --barewhich is shared on network so that other developers could also push to it so that we have our projects versioned locallybut not sure which command should I run amongst above three (or if there is some other) to track that central repo eg we push our changes from all projets to that central bare repo and pull/fetch from it too.

基本上,我创建了git init --bare一个在网络上共享的裸存储库,以便其他开发人员也可以推送到它,以便我们在本地对我们的项目进行版本控制但不确定我应该在以上三个命令中运行哪个命令(或者如果有其他命令)跟踪中央仓库,例如我们将所有项目的更改推送到中央裸仓库并从中拉取/获取。

Can anyone please enlighten on this?

任何人都可以请教一下吗?

回答by David Culp

git remote addcreates a remote, which is a shorthand name for another repository. git branch --set-upstream-tosets a branch to be tracked by the branch in the remote repository specified.

git remote add创建一个远程,这是另一个存储库的简写名称。 git branch --set-upstream-to设置要由指定远程存储库中的分支跟踪的分支。

What you are wanting to do is track a remote branch, which is done with git branch --set-upstream-toor more simply git branch -u.

您想要做的是跟踪远程分支,这是使用git branch --set-upstream-to或更简单的方式完成的git branch -u

when you clone a repository from another, a remote is created named originand the branch masteris checked out. The command to have your local branch master track the remote branch master is git branch -u origin/master, and is executed from the local master branch.

当您从另一个存储库克隆一个存储库时,会创建一个名为的远程存储库originmaster签出该分支。让本地分支 master 跟踪远程分支 master 的命令是git branch -u origin/master,并从本地 master 分支执行。

回答by VonC

In order to set the remote tracking branch with set-upstream-to, you need to define a remote repo.

为了使用 设置远程跟踪分支set-upstream-to,您需要定义一个远程仓库。

When your developers are cloning the bare repo, a remote named originis automatically defined for them. I.e, on each local clone, a git remote -vwould list a remote repo named origin, referencing the bare repo. They don't need to define a remote named upstream.

当您的开发人员克隆裸存储库时,origin会自动为他们定义一个远程命名。即,在每个本地克隆上, agit remote -v将列出一个名为 的远程仓库origin,引用裸仓库。他们不需要定义一个名为upstream.

However, that doesn't mean all the branches from that remote are tracked by a local branch.
That is where git branch --set-upstream-tocan come into play.

但是,这并不意味着来自该远程的所有分支都由本地分支跟踪。
这就是git branch --set-upstream-to可以发挥作用的地方。