如何解决在“git remote add”命令中出现“fatal: remote origin already exists.”的问题?

7

我想在我的U盘上创建一个Git仓库。我使用命令在该U盘上创建了一个裸的Git仓库:

git init --bare MyRepo

在我的笔记本电脑上的代码库(Ubuntu 14.04)中,我执行了以下命令:

git init
git add .
git commit -m 'first commit'
git remote add origin /media/alexander/Stick/MyRepo

并且遇到了错误

fatal: remote origin already exists.

我之前在U盘上成功创建了一个git仓库,没有出现任何错误(当然是使用不同的目录)。但是现在我遇到了这个错误。'git status'命令的输出为:

On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working directory clean

那么出了什么问题呢?


1
请确保您没有在错误的目录中运行 git init(在已存在的 Git 存储库中)。 - axiac
2个回答

20

先移除远程 origin,再用路径重新添加。

$ git remote rm origin
$ git remote add origin <repo-url> 

另一种方法: 设置 origin 的网址而不是添加。

$ git remote set-url origin <repo-url>

是的,也许那个方法有效。但我最初是怎么陷入这个问题的呢? - Alex
1
我猜在你执行 git init 命令时,.git 文件夹已经存在了。你能否检查一下 git init 命令的输出?如果输出类似于 “Reinitialized existing Git ...”,那么 .git 已经存在,并且 origin 也存在。 - Sajib Khan
此外,在执行操作之前,检查git remote -vgit remote show origin可以发现有什么。 - dvaergiller

0
1. create new directory name similar to that created on github on your desktop
2. copy all the files and directory to the corresponding new directory

命令:

git init

如果出现任何错误

git rm .git

将所有要推送的文件添加到队列中

git add .

检查是否全部推送正确

 git commit -m "Commit Done"

 git branch -M main

 git remote add origin https://github.com/ank247/cPeer.git
 git push -u origin main

 

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