无法更改git远程仓库origin

4
有人能解释一下为什么git remote origin不能从livepost.git更改为europeanexplorer.git吗?我尝试按照指示进行操作,但文档显示error: Could not remove config section 'remote.origin'表示该文件不存在,但这显然不是这种情况。
$ git remote -v
origin  https://github.com/harrisongill/livepost.git (fetch)
origin  https://github.com/harrisongill/livepost.git (push)
$ git remote rm origin
error: Could not remove config section 'remote.origin'
$ git remote set-url origin https://github.com/harrisongill/europeanexplorer.git
$ git remote -v
origin  https://github.com/harrisongill/livepost.git (fetch)
origin  https://github.com/harrisongill/livepost.git (push)
origin  https://github.com/harrisongill/europeanexplorer.git (push)
$ git remote rm origin
$ git remote -v
origin  https://github.com/harrisongill/livepost.git (fetch)
origin  https://github.com/harrisongill/livepost.git (push)

编辑:添加git配置

$git config --list
Harrisons-MacBook-Pro:European Explorer harrison$ git config --list
user.name=Harrison Gill
user.email=my email
remote.origin.url=https://github.com/harrisongill/livepost.git
core.repositoryformatversion=0
core.filemode=true
core.logallrefupdates=true
core.precomposeunicode=true

1
这很不寻常。请发布您的.git/config文件中的[remote "origin"]部分。 - Chris
2个回答

5

所有命令行指令都会将字符串写入到 .git/ 目录下的文件中。
远程仓库可以在 .git/config 文件中方便地配置,我认为这就是 Chris 所提到的。

因此,如果您执行 vim .git/config,您应该会看到类似于以下内容:

[core]
  repositoryformatversion = 0
  filemode = true
  bare = false
  logallrefupdates = true
  ignorecase = true
  precomposeunicode = false
[remote "origin"]
  url = git@github.com:harrisongill/livepost.git
  fetch = +refs/heads/*:refs/remotes/origin/*

[remote "origin"] 部分可能会有其他让 Git 感到困惑的内容。为什么会出现混乱?我不知道。但是手动编辑此文件应该可以解决问题 :)

去掉多余的内容,你就可以继续使用了 :)


是的,只需要直接在.git/config文件中删除[remote "origin"]后面的内容即可。谢谢@apprenticeDev。 - harrisongill

5

原始答案为2014年1月

如在“无法删除远程origin”中所述,错误消息error: Could not remove config section 'remote.origin'表示您的Git配置文件中没有远程'origin'。

至少不在您的本地配置文件中。

我刚测试了在我的全局配置文件中添加一个名为“origin”的远程部分,我得到了与您相同的结果:

C:\Users\VonC\prog\git\tests\21195717\t>git config --global --edit

C:\Users\VonC\prog\git\tests\21195717\t>git remote -v
origin  https://a.com/b (fetch)
origin  https://a.com/b (push)

C:\Users\VonC\prog\git\tests\21195717\t>git remote rm origin
error: Could not remove config section 'remote.origin'

请检查您的全局配置文件:

git config --global --edit

2016年2月更新:

Git 2.8将更新该错误消息为“no such remote”。

参见提交a31eeae提交cc8e538提交674468b提交bc60f8a(由Thomas Gummerer (tgummerer)于2016年2月16日提交)。
(由Junio C Hamano -- gitster --合并于提交ae2f255,2016年2月26日)

remote: actually check if remote exits

When converting the git remote command to a builtin in 211c89 ("Make git-remote a builtin"), a few calls to check if a remote exists were converted from:

  if (!exists $remote->{$name}) {
      [...]

to:

  remote = remote_get(argv[1]);
  if (!remote)
      [...]

The new check is not quite correct, because remote_get() never returns NULL if a name is given.
This leaves us with the somewhat cryptic error message "error: Could not remove config section 'remote.test'", if we are trying to remove a remote that does not exist, or a similar error if we try to rename a remote.

Use the remote_is_configured() function to check whether the remote actually exists, and die with a more sensible error message ("No such remote: $remotename") instead if it doesn't.


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