Git config management
Tags: git, workflow
Published: 2019-10-28 07:29:54

---
title: Git config management
published: true
tags: git, workflow
canonical_url: https://thomashartmann.dev/blog/git-config-management
cover_image: https://thomashartmann.dev/images/git-config-management.svg
---
<p>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.</p>
<p>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 <a href="https://git-scm.com/docs/git-config#_configuration_file">docs</a>, “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.</p>
<p>Furthermore, git config files ‘cascade’, and git, by default, searches four locations on your system, where later entries override earlier ones:</p>
<dl>
<dt><code>/etc/gitconfig</code></dt>
<dd>The system wide config
</dd>
<dt><code>$XDG_CONFIG_HOME/git/config</code></dt>
<dd>Falls back to <code>$HOME/.config/git/config</code> if <code>$XDG_CONFIG_HOME</code> is not set or empty.
</dd>
<dt><code>~/.gitconfig</code></dt>
<dd>Your user-specific, ‘global’ config file
</dd>
<dt><code>$GIT_DIR/config</code></dt>
<dd>Repo-specific config file
</dd>
</dl>
<p>Again, see <a href="https://git-scm.com/docs/git-config#FILES">the specific section of the documentation</a> for more information.</p>
<p>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 <code>--global</code> flag.</p>
<h2 id="setting-values">Setting values</h2>
<p>To set a value, simply run a command on the form</p>
```sh
# default (local)
git config <section>.<key> <value>
# global
git config --global <section>.<key> <value>
```
<p>For instance, to change the value of the <code>commentChar</code> entry of the <code>core</code> section to <code>;</code>:</p>
```sh
git config core.commentChar ';'
```

<h2 id="unsetting-values">Unsetting values</h2>
<p>Similarly, if you want to unset a value, use the <code>--unset</code> flag:</p>
```sh
git config --unset <section>.<key>
```

<p>So if you have overridden the <code>core.commentChar</code> value, this is the command to undo that and reset it to the default value, <code>#</code>:</p>
```sh
git config --unset core.commentChar
```

<h2 id="inspecting-config-values">Inspecting config values</h2>
<p>Sometimes, you want to know what a value has been set to. For this, pass the <code>--get</code> flag and the section and key of the value you want to look up:</p>
```sh
git config --get <section>.<key>
```
<p>So if you’re wondering what your user’s name is, for instance:</p>
```sh
git config --get user.name
```

<p>A few things to note about getting the values</p>
<dl>
<dt>Git will show you the last value it finds in the chain</dt>
<dd>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.
</dd>
<dt>The output will be empty if you have not explicitly set the value</dt>
<dd>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.
</dd>
</dl>
<h2 id="finding-the-config-value-sources">Finding the config value sources</h2>
<p>Sometimes you just want to inspect what values you’re setting in your config. You can do this by passing the <code>--list</code> flag:</p>
```sh
git config --list
```

<p>By default, this will just throw all your configuration at you, which isn’t all that helpful. However, it takes a pretty handy flag, <code>--show-origin</code>, 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.</p>
<p>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:</p>
```sh
git config --list --show-origin
```

<p>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 <code>grep</code> and look for duplicates:</p>
```sh
git config --list --show-origin | grep "user.name="
```