Thomas Hartmann
Posted on October 28, 2019
We’ve been looking a lot at how to tweak git to your liking, which is usually done through your git config file. Sometimes, though, you’ll find that the configuration isn’t working as you wanted, so I thought it’d be a good idea to look at how you can inspect, unset, and list configuration values from the command line.
Before we dive in, let’s have a few words about how your git config files work. Your config files use a specific format which, according to the docs, “consists of sections and variables. A section begins with the name of the section in square brackets and continues until the next section begins.” That’s enough for what we need right now, but go read up if you’re interested.
Furthermore, git config files ‘cascade’, and git, by default, searches four locations on your system, where later entries override earlier ones:
/etc/gitconfig
- The system wide config
$XDG_CONFIG_HOME/git/config
- Falls back to
$HOME/.config/git/config
if$XDG_CONFIG_HOME
is not set or empty. ~/.gitconfig
- Your user-specific, ‘global’ config file
$GIT_DIR/config
- Repo-specific config file
Again, see the specific section of the documentation for more information.
One last thing: All setting and unsetting of values apply only to your user configurations, and they are local—repo-specific—by default. To make them apply to your global config, pass the --global
flag.
Setting values
To set a value, simply run a command on the form
# default (local)
git config <section>.<key> <value>
# global
git config --global <section>.<key> <value>
For instance, to change the value of the commentChar
entry of the core
section to ;
:
git config core.commentChar ';'
Unsetting values
Similarly, if you want to unset a value, use the --unset
flag:
git config --unset <section>.<key>
So if you have overridden the core.commentChar
value, this is the command to undo that and reset it to the default value, #
:
git config --unset core.commentChar
Inspecting config values
Sometimes, you want to know what a value has been set to. For this, pass the --get
flag and the section and key of the value you want to look up:
git config --get <section>.<key>
So if you’re wondering what your user’s name is, for instance:
git config --get user.name
A few things to note about getting the values
- Git will show you the last value it finds in the chain
- So if you set your username in your global config, but set a different one in a repo and invoke this command from the repo, it would show you only the repo-specific one.
- The output will be empty if you have not explicitly set the value
- In other words, if it’s not in your config files, git won’t show it to you. So if you’re looking to inspect git’s default values, that’s not going to work.
Finding the config value sources
Sometimes you just want to inspect what values you’re setting in your config. You can do this by passing the --list
flag:
git config --list
By default, this will just throw all your configuration at you, which isn’t all that helpful. However, it takes a pretty handy flag, --show-origin
, which lists your configuration in two columns, with the first one being the file in which it was set and the second being the the key-value pairs.
If you ever find that some value isn’t taking effect the way you expect, this is a great way to check what values are getting set in what locations:
git config --list --show-origin
For instance, if you’ve changed your global user name, but it’s not taking effect in the current repo, you could pipe it into grep
and look for duplicates:
git config --list --show-origin | grep "user.name="
Posted on October 28, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024