简体   繁体   中英

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

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

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" .

Please note that the "XDG_CONFIG_HOME variable is set to "$HOME/.config" and the GIT_CONFIG variable is unset in my environment.

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."

But then Git should ignore the "$XDG_CONFIG_HOME/git/config" file consistently, ie while reading and writing.

Am I missing something?

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

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 .

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

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

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

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

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

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 config is done outside a repo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM