If I do git config foo.bar baz
, this adds a foo
section to .git/config
:
...
[foo]
bar = baz
...
I can remove the setting again with git config --unset foo.bar
, but the section remains in the file, with nothing in it:
...
[foo]
...
If I add another foo
setting with git config foo.bar baz
, git-config
doesn't add it to the empty foo
section; it starts a new one:
...
[foo]
[foo]
bar = baz
...
My questions are:
To answer point 3, you can use the result from git config --get-regexp
to decide when to cleanup:
git config --unset "foo.bar";
# Cleanup empty "foo" section.
# Regular expression has a trailing period to avoid testing against
# other sections that share the same prefix (e.g. "foo" vs "food").
if ! git config --get-regexp '^foo\.'; then
git config --remove-section "foo" 2> /dev/null;
fi;
There is git config --remove-section
so you could delete whole section. But yes, for me it looks like a bug that it creates a new section if an empty exists.
How do I avoid empty sections when removing a setting from .git/config ?
That should be fixed with Gti 2.18 (Q2 2018):
"git config --unset a.b
", when "a.b
" is the last variable in an otherwise empty section "a
", left an empty section "a" behind, and worse yet, a subsequent "git config a.c value
" did not reuse that
empty shell and instead created a new one.
These have been (partially) corrected.
See commit c71d8bb, commit 22aedfc, commit 6ae996f, commit 5221c31, commit 668b9ad, commit fee8572, commit 8032cc4, commit b73bdc3, commit 422e8ef (09 Apr 2018), and commit dde154b, commit 85bf5d6, commit 46fc89c, commit e931395, commit efbaca1, commit 83b7fd8 (03 Apr 2018) by Johannes Schindelin (dscho
).
(Merged by Junio C Hamano -- gitster
-- in commit 4f4d0b4, 08 May 2018)
git config --unset: remove empty sections (in the common case)
The original reasoning for not removing section headers upon removal of the last entry went like this: the user could have added comments about the section, or about the entries therein, and if there were other comments there, we would not know whether we should remove them.
In particular, a concocted example was presented that looked like this (and was added to t1300):
# some generic comment on the configuration file itself
# a comment specific to this "section" section.
[section]
# some intervening lines
# that should also be dropped
key = value
# please be careful when you update the above variable
The ideal thing for
git config --unset section.key
in this case would be to leave only the first line behind, because all the other comments are now obsolete.However, this is unfeasible, short of adding a complete Natural Language Processing module to Git, which seems not only a lot of work, but a totally unreasonable feature (for little benefit to most users).
Now, the real kicker about this problem is: most users do not edit their config files at all! In their use case, the config looks like this instead:
[section]
key = value
... and it is totally obvious what should happen if the entry is removed: the entire section should vanish.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With