如何在配置文件为“$XDG_CONFIG_HOME/git/config”时设置 Git 配置选项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24193533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:07:03  来源:igfitidea点击:

How to set a Git config option when its config file is "$XDG_CONFIG_HOME/git/config"?

gitconfig

提问by Tim Friske

With Git, version 1.9.3 on Fedora Linux, version 20, I am querying for example the user's name like so:

使用 Fedora Linux 版本 20 上的 Git,版本 1.9.3,我正在查询例如用户名,如下所示:

git config user.name
# Foo Bar

But when I try to change it like so:

但是当我尝试像这样改变它时:

git config user.name 'Bar Foo'

I get the following error:

我收到以下错误:

# error: could not lock config file .git/config: No such file or directory

In accordance to the "XDG Base Directory Specification" (http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html) and the "FILES" section of the git-config(1) manpage I placed my Git config file under "$XDG_CONFIG_HOME/git/config".

根据我放置的“XDG 基本目录规范”(http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html)和 git-config(1) 联机帮助页的“文件”部分我的 Git 配置文件在"$XDG_CONFIG_HOME/git/config".

Please note that the "XDG_CONFIG_HOMEvariable is set to "$HOME/.config"and the GIT_CONFIGvariable is unset in my environment.

请注意,在我的环境中,该"XDG_CONFIG_HOME变量已设置为"$HOME/.config"并且该GIT_CONFIG变量未设置。

I am aware that the "FILES" section of the git-config(1) manpage states: "If $XDG_CONFIG_HOME is not set or empty, $HOME/.config/git/config will be used."

我知道 git-config(1) 联机帮助页的“文件”部分指出:“如果 $XDG_CONFIG_HOME 未设置或为空,则将使用 $HOME/.config/git/config。”

But then Git should ignore the "$XDG_CONFIG_HOME/git/config"file consistently, i.e. while reading andwriting.

但是 Git 应该"$XDG_CONFIG_HOME/git/config"始终如一地忽略该文件,即在读取写入时。

Am I missing something?

我错过了什么吗?

回答by torek

You're trying to set it locally (in .git/config) rather than globally (in the XDG config in your home directory).

您正在尝试在本地(在.git/config)而不是全局(在您的主目录中的 XDG 配置中)设置它。

The error occurs because you're not actually in a repository. It's a bit of a bogus error message but I can repeat it:

发生错误是因为您实际上不在存储库中。这有点虚假的错误信息,但我可以重复一遍:

$ cd /someplace/with/no/repo; git config user.name boo
error: could not lock config file .git/config: No such file or directory

As Biffen already mentioned in a comment, you need to add --global.

正如Biffen 在评论中已经提到的,您需要添加--global.

As VonC notes, the misleading error message is changed to something sensible as of git version 2.8.

正如VonC 所指出的,从 git 2.8 版开始,误导性错误消息已更改为合理的内容。

回答by VonC

Note that, if you forget --global, the error message will be more explicit with git 2.8 (March 2016).

请注意,如果您忘记--global了 git 2.8(2016 年 3 月),错误消息将更加明确。

See commit 638fa62(24 Feb 2016) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster--in commit d3faba8, 26 Feb 2016)

请参阅Johannes Schindelin ( ) 的提交 638fa62(2016 年 2 月 24 日(由Junio C Hamano合并-- --d3faba8 提交中,2016 年 2 月 26 日)dscho
gitster

git config: report when trying to modify a non-existing repo config

It is a pilot error to call git config section.key valueoutside of any Git worktree.
The message

git config: 尝试修改不存在的 repo 配置时报告

git config section.key value在任何 Git 工作树之外调用是一个试点错误。
消息

error: could not lock config file .git/config: No such file or directory

is not very helpful in that situation, though. Let's print a helpful message instead.

但是,在这种情况下不是很有帮助。让我们打印一条有用的消息。

So at least now, you know when a git configis done outsidea repo.

所以至少现在,你知道什么时候回购之外git config完成了。