Git错误 - 密钥不包含部分

16

我最近将一个Git仓库转移到了新的组织中。我运行了以下命令:

git remote set-url origin https://github.com/organizationname/therepo.git

我已经成功地从新的位置拉取/推送了代码。但是每次我运行Git命令时都会出现以下错误:

error: key does not contain a section: repositoryformatversion
error: key does not contain a section: filemode
error: key does not contain a section: bare
error: key does not contain a section: logallrefupdates
error: key does not contain a section: ignorecase
error: key does not contain a section: precomposeunicode

我最初认为这与我的配置文件有关,但那些字段都存在。 我的 /.git/config 文件的前几行看起来像这样:

repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true

这个答案中建议检查--get-regex,但是我在我的配置文件或.gitconfig文件中没有看到任何与之相关的引用。看起来我有2个git配置文件:/usr/local/git/etc/gitconfig/Users/chmd/.gitconfig。我尝试将这些键添加到/Users/chmd/.gitconfig文件中,但没有成功。我错过了哪一步来清除这些错误?基于之前的答案和研究,它似乎是我的配置问题,但是我已经在我的gitconfig中加入了这些字段?
7个回答

18

问题实际上出现在.git/config文件中。您可能错误地编辑了它并删除了部分名称。

Git配置文件中的值被分组为节。每个节的名称都放置在方括号之间,位于单独的一行上。

您发布的值(从您的.git/config开头)应该保留在core节中。请确保您的.git/config文件如下:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ...

啊哈!就是这个问题。我没有意识到远程更改会影响到那个。将来我会记住的。 - kawnah
直到之后我才遇到了这个问题,我真的不知道发生了什么事情... - kawnah
@kawnah 你确定你没有手动编辑过.git/config文件吗?如果正确使用Git的话,这个文件以这种方式结束是极其不可思议的。或者,也许你正在使用一个不使用git config来处理配置文件的GUI客户端,它破坏了你的文件。 - axiac
@axiac 我不会编辑那个文件。我使用Atom作为我的文本编辑器。也许它做了什么? - kawnah
@axiac,您能发布.git/config的所有内容吗?我的配置文件与您发布的相似。 - Khem Raj Regmi

14
将以下代码粘贴到终端,它将以VI文本编辑器打开全局配置文件。 ```bash sudo vi /etc/gitconfig ```
git config --global --edit

按i键以在VI文本编辑器中输入并删除所有文本,然后粘贴以下内容

[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true

要在VI中保存,请按ESC,然后输入:wq并按回车键


9
这不是你的情况,但是我在这里回应其他遇到和我一样问题的人。我收到了这个错误信息:
error: key does not contain a section: name
error: key does not contain a section: email

事实证明,我在~/.gitconfig文件中弄错了一些东西。

为了解决这个问题,我查看了这个示例并意识到该文件的第一个部分应该像这样(虚假示例数据):

[user]
    name = Bart S
    email = bart.s@example.com
    username = barts

这解决了我的问题。

4

1

密钥不包含

全局

如果您遇到此类错误,那是因为您在--global之前没有加入双破折号 (--),像这样输入了global关键字:

错误的写法:

git config global diff.tool vscode

正确:

git config --global diff.tool vscode

希望这对你有所帮助。


1

git配置[section]解决方案

解决git [section]警告

  1. 全局配置
$ vim ~/.gitconfig

[user]
  email = xgqfrms@xgqfrms.xyz
  name = xgqfrms



$ cat ~/.gitconfig


项目本地配置。
$ vim .git/config

[core]
  repositoryformatversion = 0
  filemode = true
  bare = false
  logallrefupdates = true

[user]
  name = xgqfrms
  email = xgqfrms@ufo.com

$ cat .git/config

演示

error: key does not contain a section: email
error: key does not contain a section: name

before

enter image description here

after

enter image description here


0
错误:密钥不包含用户部分 在执行以下操作时遇到了这个错误:
git config --list user

我所要做的就是做:
git config --list user.name

或者

git config --list user.email

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