在将代码推送到新的代码仓库时,出现了“远程源已经存在”的错误提示。

673

我的项目在GitHub上的某个位置:git@github.com:myname/oldrep.git

现在我想将所有代码推送到另一个位置上的新存储库,git@github.com:newname/newrep.git

我使用了以下命令:

git remote add origin git@github.com:myname/oldrep.git

但是我收到了这个错误信息:

致命错误:远程 origin 已经存在。


7
该命令的输出结果是列出当前 Git 仓库配置的所有远程存储库的详细信息,包括它们的远程存储库名称和 URL 地址。 - sykora
可能是重复的问题,参考 Github "fatal: remote origin already exists" - That Brazilian Guy
1
一个好的方法是在你新创建的仓库底部使用 “从另一个仓库导入”,如果你知道旧仓库的URL。 - JW.ZG
这里曾经有一个类似的问题:https://dev59.com/OnE95IYBdhLWcg3wKqok - jciloa
1
如果您想保留原始远程,只需使用不同的名称 git remote add origin2 .... 即可。但是,如果您只需要推送一次而不修改存储库配置,则可以简单地执行 git push git@github.com:user/another-project.git master:master - ccpizza
22个回答

1052

你会看到这个错误是因为 "origin" 不可用。"origin" 是命令中的一个惯例,而非命令的一部分。"origin" 是远程代码库的本地名称。

例如,您也可以编写:

git remote add myorigin git@github.com:myname/oldrep.git  
git remote add testtest git@github.com:myname/oldrep.git

请参阅手册:

http://www.kernel.org/pub/software/scm/git/docs/git-remote.html

要删除远程存储库,请输入:

git remote rm origin

再次提醒,如果你想要删除"upstream"远程仓库,"origin"是指代远程仓库的名称:

git remote rm upstream

13
"git remote rm origin"这个命令对我没有起作用,如果对你也不行,试着用 "git remote -v" 命令查看你的 origin 是否有设置一个 URL,如果没有,可能是因为你在本地进行了初始化并尝试将其推送到远程,像我一样犯了一些错误。然后按照 RobinH 的答案操作: git remote set-url origin git@github.com:username/projectname.git - Clarence Liu
1
请查看此答案以强制更改URL:https://dev59.com/questions/82Yr5IYBdhLWcg3wka9M - srodriguex
1
"git remote rm origin" 像魔法一样奏效,太棒了! :) 哎呀,我对 git 的术语还很陌生,所以搜索起来比较费劲,但是你的答案帮了我很多。 :) 谢谢! - Martin Pfeffer
5
git push -u origin master --force - Ronnie Royston
1
git remote rm origin 对我有用。 - kkgarg
显示剩余2条评论

300

前面的解决方案似乎忽略了源,他们只建议使用另一个名称。当你想要使用git push origin时,请继续阅读。

问题出现是因为按照错误的Git配置顺序进行操作。您可能已经在.git配置文件中添加了'git origin'。

您可以使用以下行更改Git配置中的远程origin:

git remote set-url origin git@github.com:username/projectname.git

这个命令设置一个新的URL,以便将Git存储库推送到其中。重要的是填入你自己的用户名项目名称


1
这对我解决了问题。但真正帮助我解决这个问题的是因为我使用了随Github for Windows安装的portableGit。我在这里找到了解决方案。 - PerseP
1
太好了!我需要将我的Gitorious存储库更改为GitLab存储库,这个解决方案非常完美!谢谢! - Marcelo dos Santos
1
和原帖作者情况一样,我也需要先这样做,然后使用 @MrHus 的解决方案。 - Sean the Bean
2
如果有人遇到权限错误,可能需要像我一样使用https版本。它应该是这样的:git remote set-url origin https://github.com/<username>/<projectname>.git - Justin Liu

99
如果您错误地将本地名称命名为“origin”,则可以使用以下方法删除它:
git remote rm origin

1
“错误地将本地名称命名为'origin'”实际上是什么意思?你能详细解释一下吗?@Ozgur - Yuan Wen
1
这可能意味着您添加了不指向 git 存储库的远程源。因此,如果您计划将所有更改推送到主分支,则 git 将抱怨说远程源不是 git 存储库。 - Gautham Honnavara

29

方法1->

由于原始数据已经存在,因此需要将其删除。

git remote rm origin
git remote add origin https://github.com/USERNAME/REPOSITORY.git

METHOD2->

可以使用git remote set-url命令更改现有的远程仓库URL。

如果您正在更新以使用HTTPS,则:

git remote set-url origin https://github.com/USERNAME/REPOSITORY.git
如果您正在更新以使用SSH
git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
如果尝试更新不存在的远程,您将收到错误消息。所以要小心。METHOD3 -> 使用git remote rename命令重命名现有的远程。一个现有的远程名称,例如origin。
git remote rename origin startpoint
# Change remote name from 'origin' to 'startpoint'

验证远程的新名称 ->

git remote -v

如果您是Git新手,请尝试这个教程->

尝试Git教程


17

你可以在文本编辑器中简单地编辑你的配置文件。

~/.gitconfig 中,你需要输入类似以下内容:

[user]
        name  = Uzumaki Naruto
        email = myname@example.com

[github]
        user = myname
        token = ff44ff8da195fee471eed6543b53f1ff

在你的代码库的配置文件oldrep/.git/config中:

[remote "github"]
        url = git@github.com:myname/oldrep.git
        push  = +refs/heads/*:refs/heads/*
        push  = +refs/tags/*:refs/tags/*

如果你的仓库配置文件中有一个远程部分,且URL匹配,你只需要添加推送(push)配置。如果你使用公共URL进行拉取(fetching),你可以将推送(push)的URL放入'pushurl'中(警告:这需要刚发布的Git 1.6.4版本)。


15

我曾经也遇到这个问题,做了一些研究后解决了:

  1. 下载GitHub for Windows或类似的软件,其中包括一个shell。
  2. 从任务菜单中打开Git Shell。这将打开一个包含Git命令的PowerShell。
  3. 在shell中切换到您的旧存储库,例如:cd C:\path\to\old\repository
  4. 显示旧存储库的状态。
  • Type git remote -v to get the remote path for fetch and push remote. If your local repository is connected to a remote, it will show something like this:

     origin  https://user@bitbucket.org/team-or-user-name/myproject.git (fetch)
     origin  https://user@bitbucket.org/team-or-user-name/myproject.git (push)
    
  • If it's not connected, it might show origin only.

  1. Now remove the remote repository from the local repository by using

    git remote rm origin
    
  2. Check again with git remote -v, as in step 4. It should show origin only, instead of the fetch and push path.

  3. Now that your old remote repository is disconnected, you can add the new remote repository. Use the following to connect to your new repository:

注意:如果您正在使用Bitbucket,则首先需要在Bitbucket上创建一个项目。创建后,Bitbucket将显示所有必需的Git命令,以将存储库推送到远程,这些命令看起来类似于下面的代码片段。但是,这对其他存储库也适用。

cd /path/to/my/repo # If you haven't done that yet.
git remote add mynewrepo https://user@bitbucket.org/team-or-user-name/myproject.git
git push -u mynewrepo master # To push changes for the first time.

这就是全部。


13
  1. git remote rm origin

  2. git remote -v 不会显示任何仓库名称

  3. git remote add origin git@github.com:username/myapp.git

  4. git push origin master 它将开始创建新分支的过程。 您可以看到您的工作已经被推送到Github。


9
git remote rm origin
git remote add origin git@github.com:username/myapp.git

9
以下两个命令应该有助于设置。
git remote set-url origin https://github.com/USERNAME/NEW_REPO.git
    
git push --set-upstream origin main

8

您无需删除现有的“origin”远程,只需使用名称不同于“origin”的远程添加即可,例如:

git remote add github git@github.com:myname/oldrep.git


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接