Photo by Wilhelm Gunkel on Unsplash

Git-Config Essentials

How to Set and Inspect Baseline Configuration Values

Karl Stolley
4 min readJan 12, 2022

--

📚 Connect with us. Want to hear what’s new at The Pragmatic Bookshelf? Sign up for our newsletter. You’ll be the first to know about author speaking engagements, books in beta, new books in print, and promo codes that give you discounts of up to 40 percent.

Every Git installation needs configuring. The git-config documentation clocks in at a novella-length 35,000 words. So over a series of posts in 2022, we’ll look at numerous little-known but useful configuration values for Git. The goal of this series is not just to add lines to your ~/.gitconfig, but to change how you work with Git—while taming some of its weirder, mildly irritating default behaviors.

Setting Baseline Git Configuration Values

In this post, let’s look at the basics. If you’ve run Git on the command line, you’ve probably used git config to set some essential configuration values.

At minimum, you should set your user.name and user.email, which Git attaches to your commits:

$ git config --global user.name "Darth Vader"
$ git config --global user.email "vader@deathstar.example.com"

That used to suffice. But beginning with version 2.27, Git will throw a finger-wagging warning if you attempt to interact with a remote branch whose changes differ from your own:

warning: Pulling without specifying how to reconcile divergent branches is discouraged. You can squelch this message by running one of the following commands sometime before your next pull:  git config pull.rebase false  # merge (the default strategy)
git config pull.rebase true # rebase
git config pull.ff only # fast-forward only

We’ll look at the pull.rebase and pull.ff configuration values—and what they mean for you as a developer—in a future post. If you suffer raging anxiety from casual mentions of the word rebase, go with the default strategy for now:

$ git config --global pull.rebase false

And finally, since Git version 2.28, it’s also necessary to set a default branch name for new repos created via git init. Previously, master was the default, which Git, GitHub, and the broader industry are moving away from because of its racist connotations. Git itself suggests main, trunk, or development. Set your preference on the init.defaultBranch property:

$ git config --global init.defaultBranch main

Great. Configuration. Got it. But where does Git store those values? How can you check what values you’ve set? Let’s see how to inspect your configuration values and where Git stores them.

Inspecting Your Configuration Values

Photo by Markus Winkler on Unsplash

The git config command doesn’t only set configuration values.

You can read configuration values with it, too.

To list your --global values, pass in the --list option:

$ git config --global --list
user.name=Darth Vader
user.email=vader@deathstar.example.com
init.defaultBranch=main
pull.rebase=false

And if you like, you can add another option, --show-origin, to output the source file containing each value:

$ git config --global --list --show-origin file:/home/vader/.gitconfig user.name=Darth Vader
file:/home/vader/.gitconfig user.email=vader@deathstar.example.com
file:/home/vader/.gitconfig init.defaultBranch=main
file:/home/vader/.gitconfig pull.rebase=false

That information is a little redundant here. Later in this series, we’ll look at configuration values stored at different levels of scope: not just per user (--global), but on system and per-repository levels, too.

Summing Up

In this post, we’ve dealt with:

  • Setting user.name and user.email
  • Setting pull.rebase to false globally to deal with remote branches
  • Setting a default branch name
  • Inspecting your configuration values

In the next post, we’ll look at why your config output might not be output directly on the command line, as in the examples above, and what you can do about it.

Also by Karl Stolley:

Karl Stolley’s book, Programming WebRTC: Build Real-Time Streaming Applications for the Web, is available in beta from The Pragmatic Bookshelf. You can save 35 percent with promo code git_config_2022 now through March 1, 2022. Promo codes are not valid on prior purchases.

--

--

Karl Stolley
The Pragmatic Programmers

Author of Programming WebRTC, out in beta at Pragmatic Programmers. Chicagoan. Developer & writer.