bash “git remote -v”显示 (fetch) 和 (push) 两次,一次用于“github”,一次用于“origin”,这是什么意思?

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

"git remote -v" shows (fetch) and (push) twice, once for 'github' and once for 'origin' what does that mean?

gitbashgithubpushgit-bash

提问by Srujan Barai

Not sure if its the right place for asking this question, I was unable to understand something and googling didn't help.

不确定它是否是问这个问题的正确地方,我无法理解某些东西,谷歌搜索也没有帮助。

I have been referring gittutorials, (obviously meaning i'm new to it).

我一直在参考git教程,(显然意味着我是新手)。

I learnt the command git remote -vto check the remote status. Well, all the git tutorial had snapshots like this, showing the result:

我学会了git remote -v检查远程状态的命令。好了,所有的git的教程民政事务快照像这样,显示结果:

origin  https://github.com/something/something-else.git (fetch)
origin  https://github.com/something/something-else.git (push)

But when I tried the command I am getting similar result for originas well as github. Something like this:

但是,当我试图命令我得到类似的结果为origin,以及github。像这样的东西:

github  https://github.com/srujan7/something-something-url.git (fetch)
github  https://github.com/srujan7/something-something-url.git (push)
origin  https://github.com/srujan7/something-something-url.git (fetch)
origin  https://github.com/srujan7/something-something-url.git (push)

Problem:

问题:

I am not sure what does this githuband originhere means. Why did I get it twice? (unlike the tutorial)I am also not sure if did anything wrong, or its perfectly right. Pointing me to some other tutorial explaining this will also do.

我不确定 thisgithuboriginhere 是什么意思。为什么我收到了两次?(与教程不同)我也不确定是否做错了什么,或者完全正确。将我指向其他一些解释这一点的教程也可以。

Feel free to suggest edits.

随时提出修改建议。

采纳答案by torek

Git supports multiple remotes, so this is a normal enough state. Edit: as sschuberth answered, it's notquite normal to have the same URL for multiple remotes. It's not harmfulbut you might as well delete one of them.

Git 支持多个遥控器,所以这是一个足够正常的状态。 编辑:正如sschuberth 回答那样,多个遥控器具有相同的 URL 不太正常。这无害,但您不妨删除其中之一。

What is a remote?

什么是遥控器?

A remote is just a name, like originor upstreamor githubor even fredor srujan.

远程仅仅是一个名称,如originupstreamgithub或甚至fredsrujan

Git stores each such name in a configuration file. Under that name, Git can then store additional information, such as one or more URLs. (For a remote to be useful it needs to have at least one URL, stored as its urlsetting.) For fetching, Git normally needs at least one fetchsetting per remote as well.

Git 将每个这样的名称存储在一个配置文件中。然后,在该名称下,Git 可以存储其他信息,例如一个或多个 URL。(要使遥控器有用,它需要至少有一个 URL,作为其url设置存储。)对于获取,Git 通常fetch每个遥控器也至少需要一个设置。

When git remote -vlists two or more remotes

git remote -v列出两个或更多遥控器时

Most commonly, each of your Git repositories will have just one remote, usually named origin. This is because when git clonecreates a new repository by copying some existing repository, it records (in the new repository) the URL of the existing repository. To record this, it creates a remote, and by default, it uses the name originfor that remote.

最常见的是,您的每个 Git 存储库都只有一个远程,通常命名为origin. 这是因为当git clone通过复制一些现有存储库来创建新存储库时,它会记录(在新存储库中)现有存储库的 URL。为了记录这一点,它创建了一个遥控器,默认情况下,它使用该origin遥控器的名称。

The git remote addsub-command adds additional remotes. You specify a name for the remote and a URL, and Git records the new remote name and sets its main URL to the one you just gave.

git remote add子命令添加了额外的遥控器。您指定远程名称和 URL,Git 会记录新的远程名称并将其主 URL 设置为您刚刚提供的名称。

After this, git remoteor git remote showwill list the two (or more) remotes you have set up.

在此之后,git remotegit remote show将列出您设置的两个(或更多)遥控器。

How do I use a remote?

如何使用遥控器?

When you run git fetchor git push, the very next word is normally the name of the remote to fetch from or push to. For instance, git fetch originfetches from the remote named origin.

当您运行git fetch或 时git push,下一个词通常是要从中获取或推送到的远程名称。例如,git fetch origin从名为 的远程获取origin

If you have two remotes, one named fred and one named srujan, you can git fetch fredor git fetch srujan. Similarly, you can git push fredor git push srujan. These will contact the specified remote, using the URL stored under that remote.

如果您有两个遥控器,一个名为 fred,一个名为 srujan,您可以git fetch fredgit fetch srujan。同样,您可以git push fredgit push srujan。这些将使用存储在该遥控器下的 URL 联系指定的遥控器。

When fetchingfrom a remote, your Git will copy their Git's branches, but renamethem so that they are unique to that particular remote. For instance, if I fetch from remote srujan, and srujan's Git (at the specified URL) has branches masterand develop, I will get remote-trackingbranches named srujan/masterand srujan/develop. If I then fetch from remote fred, I will get remote-tracking branches like fred/masterand fred/develop.

当从远程获取时,您的 Git 将复制其 Git 的分支,但重命名它们以便它们对于该特定远程是唯一的。例如,如果我从 remote 获取srujan,并且 srujan 的 Git(在指定的 URL 处)有分支masterand develop,我将得到名为and 的远程跟踪分支。如果我然后从 remote 获取,我将获得远程跟踪分支,如和。srujan/mastersrujan/developfredfred/masterfred/develop

What about git pull?

怎么样git pull

The git pullcommand is meant to be a convenient shorthand for git fetchfollowed by git merge. Like git fetch, git pulltakes a third word, which is the name of the remote. The main thing git pulldoes with this is hand it off to git fetch.

git pull命令旨在成为git fetch后跟的便捷简写git merge。像git fetch,git pull需要第三个词,即遥控器的名称。git pull与此有关的主要事情是将其交给git fetch.

The weird thing about git pullis that it also takes branch names. It uses those in an odd way: git pull srujan mastermeans "run git fetch srujanfirst, then run git merge surjan/master".

奇怪的git pull是它也需要分支名称。它以一种奇怪的方式使用这些:git pull srujan master意思是“git fetch srujan先运行,然后运行git merge surjan/master”。

You're usually better off just running git fetchyourself, at least at first, because it's possible for eitherthe fetch step to fail (if your network connection goes down, for instance) orthe merge step to fail (if the merge cannot be done automatically). Until you are very familiar with Git, I believe you are better off knowing precisely whichstep has gone wrong, because you will need to take different actions to fix it, depending on which step failed.

通常你最好只是运行git fetch自己,至少在第一,因为它可能要么取指步骤就是失败的(如果您的网络连接出现故障,例如)合并步骤就是失败的(如果合并不能被自动完成)。在您非常熟悉 Git 之前,我相信您最好准确地知道一步出错了,因为您将需要采取不同的措施来修复它,这取决于哪一步失败了。

You may also want to rebase rather than merging. This is, if anything, more complex than merging (although often a better approach too). You canget git pullto do a rebase instead of a merge, and in fact you can make this happen automatically, but the details are a little bit complicated, and manually running git fetchfirst, and then git rebasesecond, is notcomplicated at all.

您可能还想变基而不是合并。如果有的话,这比合并更复杂(尽管通常也是更好的方法)。你可以得到git pull做底垫,而不是合并,实际上可以让这个自动发生,但细节有点复杂,并手动运行git fetch,然后再git rebase第二,是不是在所有复杂。

(In short, don't use git pulluntil you are very familiar with fetching, merging, and rebasing, and are ready to allow Git to try to do them all at once.)

(简而言之,在git pull您非常熟悉获取、合并和变基,并准备好让 Git 尝试一次性完成所有这些操作之前,请不要使用。)

回答by sschuberth

In Git, a "remote" basically just is an alias for a server URL, so you don't have to type the full URL all the time. If you don't explicitly specify a remote name, the default name is origin. In your case, you have two remotes, originand github, pointing to the same URL (for whatever reason; you probably followed some tutorial steps to create the githubremote for demonstration purposes). So one of the two is superfluous. As githubis the non-standards name, I'd simply remote that remote again by typing

在 Git 中,“远程”基本上只是服务器 URL 的别名,因此您不必一直输入完整的 URL。如果您没有明确指定远程名称,则默认名称为origin. 在您的情况下,您有两个遥控器,origingithub,指向相同的 URL(无论出于何种原因;您可能按照一些教程步骤来创建github遥控器以进行演示)。所以两者之一是多余的。正如github非标准名称一样,我只需通过键入再次远程远程

git remote rm github