How to avoid empty sections when deleting a parameter from .git / config?

If I do git config foo.bar baz , this adds the foo section to .git/config :

 ... [foo] bar = baz ... 

I can delete the setting again using git config --unset foo.bar , but the section remains in the file, there is nothing in it:

 ... [foo] ... 

If I add another foo parameter with git config foo.bar baz , git-config does not add it to the empty foo section; he starts a new one:

 ... [foo] [foo] bar = baz ... 

My questions:

  • Is this expected behavior?
  • If not, is this a mistake?
  • Is there a way to avoid the possible spread of empty sections in the configuration file when the configuration is disabled?
+4
source share
3 answers

Thomas Rast got my attention on this thread on the git developers mailing list .

According to my understanding of Peff's description, the problem could be easily fixed if the programming of the configuration analyzer were not so ad-hoc. After analyzing the file, the result is a structure that contains configuration parameters, but not one of the original file structure. Since the partition structure information is not available to callers, the installation setup code cannot know if there is an empty partition with the right header. Also, deleting empty sections is a bit more complicated, because they may contain important comments that should not be automatically deleted just because the last functional part of the section has been deleted.

Output:

  • No, this is not the expected behavior;
  • Yes, this is a mistake.

This kind of programming is tedious, but simple, so I'll see if I can handle the fix.

+1
source

To answer point 3, you can use the result from git config --get-regexp to decide when to clear:

 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 (eg "foo" vs "food"). if ! git config --get-regexp '^foo\.'; then git config --remove-section "foo" 2> /dev/null; fi; 
+4
source

There is git config --remove-section , so you can delete the entire section. But yes, for me it seems like an error that creates a new partition if there is an empty one.

+3
source

All Articles