Git - 致命错误:远程仓库 origin 已经存在

26

我无法通过remote命令远程创建源代码库:

$ git remote add origin https://github.com/LongKnight/git-basics.git
fatal: remote origin already exists.
为了解决错误,我尝试了这个:
$ git remote -v origin
$ git remote -v show origin

我的本地代码库没有将文件上传到远程服务器:

$ git push -u origin master
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

每个代码库都有自己的源吗?


解决方案:我之前使用的是 Github 或者 Git Shell 自带的 Powershell 进行教程,一旦我切换到 Git Bash ,它就可以正常工作了。


2
你的问题非常不结构化,不清楚你具体在问什么。如果你有多个问题,请分开提问。目前我不确定你想知道什么。 - Sascha Wolf
抱歉,我尽可能提供了足够的信息供大家使用,每个仓库都有自己的起源吗? - Brad Thrumble
@BradThrumble 不要介意。需要一段时间来适应该网站的格式。尽可能简明扼要地表达你的问题。在它被投票降至谷底之前,你还有时间改进你的问题。 - jub0bs
是的,这都是学习如何在网站上完成任务的一部分。谢谢你的回复。 - Brad Thrumble
我卡了8个多小时,直到我尝试使用Git Bash。 - Mike
可能是重复的问题,参考 Github "fatal: remote origin already exists" - ranieribt
4个回答

79

稍微简单一些:

git remote set-url origin https://github.com/LongKnight/git-basics.git

这将用一个新的起源来替换当前的。


是的,这看起来更好。 - Mayur Nagekar

1

我遇到了类似的问题,但是通过以下方法解决了:

git remote set-url origin https://GitHub.com/Fasunle/my_portfolio.git

然后,

git push main master 

它起作用了。

为了使用git push,您必须指定最终目的地,然后是本地分支(在我的情况下,本地分支是主分支,远程分支是main)。但它们也可以相同。例如:

git push -u main local_branch_to_push

或者

git push -u master local_branch_to_push

1

尝试这个:

git remote rm origin

然后,

git remote add origin https://yourLink


为什么?这就是 git remote set-url 的作用。 - VonC

0

嗯。

为什么您的起点没有值,这很奇怪。通常情况下,它应该是这样的:

[mayur.n@harry_potter]$ git remote -v
origin  /mnt/temp.git (fetch)
origin  /mnt/temp.git (push)

你的origin没有与之关联的URL,实际上是名称和值对。所以当你说“git push origin master”时,Git会替换origin的值。在我的情况下,它将是“/mnt/temp.git”。

现在你可以怎么做呢?

尝试这个:

1)在另一个目录中克隆存储库。

2)运行“git remote -v”并获取origin的值。

3)在你的情况下,它看起来像是“https://github.com/LongKnight/git-basics.git

4)现在回到你的工作目录,运行“git remote add origin2 https://github.com/LongKnight/git-basics.git

5)运行“git remote remove origin

6)现在运行“git remote rename origin2 origin

7)使用“git remote -v”检查origin的值

8)现在应该已经正确设置了。如果是这样,请运行“git push


谢谢你的回复,Mayur。我尝试了,但仍然遇到了同样的问题。 - Brad Thrumble
git remote -v 会显示什么? - Mayur Nagekar
它仍然显示为 origin,我尝试使用 Git Bash 而不是 GitHub 的 PowerShell,第一次就成功了,获取和推送看起来就像你说的那样,我应该避免使用 GitHub 客户端吗? - Brad Thrumble
你使用的是Windows操作系统吗?你用什么工具进行Git操作? - Mayur Nagekar
Windows 7,我下载了Git和GitHub,并且都安装好了,当时我真的不知道我在做什么。 - Brad Thrumble

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