我试图按照迈克尔哈特尔的Rails教程,但我遇到了一个错误。

我在GitHub注册了,发放了一个新的SSH密钥,并创建了一个新的存储库。但是当我在终端中输入下一行时,我得到了以下错误:

Parkers-MacBook-Pro:.ssh ppreyer$ git remote add origin git@github.com:ppreyer/first_app.git
fatal: remote origin already exists.

只是想知道是否有人遇到过这个问题?


当前回答

要查看您有多少个别名以及它们是什么,您可以启动这个命令 Git remote -v

然后查看您在哪个存储库中 那就试试 git remote set-url——add[然后你的存储链接] Git push -u origin master

其他回答

短版:

您只需更新现有的远程:

$ git remote set-url origin git@github.com:ppreyer/first_app.git

长版:

正如错误消息所示,已经有一个配置了相同名称的远程服务器。因此,您可以使用不同的名称添加新的远程设备,如果不需要的话,也可以更新现有的远程设备。

要添加一个新的远程,例如,调用github而不是origin(显然已经存在于您的系统中),执行以下操作:

$ git remote add github git@github.com:ppreyer/first_app.git

记住,尽管在教程中你看到“起源”,你应该把它换成“github”。例如$ git push origin master现在应该是$ git push github master。

但是,如果你想查看已经存在的原始远程是什么,你可以执行$ git remote -v。如果你认为这是由于一些错误,你可以这样更新它:

$ git remote set-url origin git@github.com:ppreyer/first_app.git

该错误消息表明您的git目录中已经有一个远程服务器。 如果您对那个遥控器感到满意,您就可以推送您的代码。如果没有或者你不能推,就:

git remote remove origin
git remote add origin git@github.com:ppreyer/first_app.git

瞧!

$ git remote add origin git@gitlab.com:abc/backend/abc.git In this command origin is not part of command it is just name of your remote repository. You can use any name you want. First You can check that what it contains using below command $ git remote -v It will gives you result like this origin git@gitlab.com:abc/backend/abc.git (fetch) origin git@gitlab.com:abc/backend/abc.git (push) origin1 git@gitlab.com:abc/backend/abc.git (fetch) origin1 git@gitlab.com:abc/backend/abc.git (push) if it contains your remote repository path then you can directly push to that without adding origin again If it is not contaning your remote repository path Then you can add new origin with different name and use that to push like $ git remote add origin101 git@gitlab.com:abc/backend/abc.git Or you can rename existing origin name add your origin git remote rename origin destination fire below command again $ git remote -v destination git@gitlab.com:abc/backend/abc.git (fetch) destination git@gitlab.com:abc/backend/abc.git (push) It will change your existing repos name so you can use that origin name Or you can just remove your existing origin and add your origin git remote rm destination

remote的概念就是远程存储库的URL。

原点是指向该URL的别名。因此,我们不需要在每次想要将东西推送到存储库时都写入整个URL,只需使用这个别名并运行:

Git push -u origin master

告诉git将我们的代码从本地主分支推到远程源存储库。

每当我们克隆一个存储库时,git默认为我们创建这个别名。此外,每当我们创建一个新的存储库时,我们只是自己创建它。

不管它是什么情况,我们总是可以把这个名字改为任何我们喜欢的,运行这个:

git remote rename [current-name] [new-name]

由于它存储在git应用程序的客户端(在我们的机器上),更改它不会影响我们的开发过程,也不会影响我们的远程存储库。记住,它只是一个指向地址的名称。

这里通过重命名别名所做的唯一更改是,每次将内容推入存储库时都必须声明这个新名称。

Git push -u my-remote-alias master

显然,一个名字不能指向两个不同的地址。这就是为什么您会得到这个错误消息。在本地机器上已经有一个名为origin的别名。要查看你有多少个别名,它们是什么,你可以启动这个命令:

git remote -v

这将显示您拥有的所有别名以及相应的url。

你也可以删除它们,如果你喜欢运行这个:

git remote rm my-remote-alias

简而言之:

看看你已经有了什么, 删除或重命名它们, 添加您的新别名。

快乐的编码。

如果在没有初始化git的情况下在directory中运行命令,也会发生这种情况。如果是这种情况,首先运行:

git init