How to Check If a Git Repository Has a Specific Config Setting

GitBeginner
Practice Now

Introduction

In this lab, you will learn how to check specific configuration settings within a Git repository. We will begin by using the git config command to query individual settings, understanding how to retrieve values like your global user name and email.

Next, we will explore the different levels of Git configuration, specifically focusing on the distinction between global and local settings. You will learn how to list all your configurations at both levels to see how they are applied and how local settings can override global ones. Finally, we will cover how to handle situations where a requested configuration value might be missing.

Use git config to Query Setting

In this step, we will learn how to use the git config command to query Git settings. Git uses configuration files to store settings that control how it behaves. These settings can be applied globally (to all your Git repositories) or locally (to a specific repository).

First, let's check a global setting. We configured your name and email globally in the setup section. You can query your global user name using the --global flag and the setting name user.name:

git config --global user.name

You should see the name you configured in the setup:

Jane Doe

Now, let's query your global user email:

git config --global user.email

You should see the email you configured:

jane.doe@example.com

These commands show you the values that Git will use by default for your commits across all repositories on this system. Understanding how to query these settings is the first step in managing your Git configuration.

Check Global and Local Configs

In this step, we will explore the difference between global and local Git configurations and how to view all your settings.

Git has different levels of configuration. The most common are:

  • System level: Applies to all users on the system. (Less common for individual users)
  • Global level: Applies to your user account on the system, for all your repositories. This is stored in ~/.gitconfig.
  • Local level: Applies only to the current repository. This is stored in .git/config inside the repository.

Local configurations override global configurations, and global configurations override system configurations.

To view all your global settings, you can use the --global --list flags:

git config --global --list

You should see output similar to this, showing the user.name and user.email we set up, and the init.defaultBranch:

user.name=Jane Doe
user.email=jane.doe@example.com
init.defaultBranch=master

Now, let's create a local configuration within our my-time-machine repository. Navigate back into the repository directory:

cd ~/project/my-time-machine

We can set a local user name that will only apply to this specific repository. Let's use a different name:

git config user.name "Time Traveler"

Notice we didn't use the --global flag this time. This tells Git to set the configuration locally for the current repository.

Now, let's view the local configurations for this repository using --local --list:

git config --local --list

You should see the local user name we just set:

user.name=Time Traveler

Finally, let's view all configurations that apply to the current repository, including both local and global settings. Git will show the local settings first, as they take precedence:

git config --list

The output will show both local and global settings. Notice that user.name shows "Time Traveler" (the local setting) and user.email shows "jane.doe@example.com" (the global setting, as we didn't set a local email):

user.name=Time Traveler
user.email=jane.doe@example.com
init.defaultBranch=master

This demonstrates how Git prioritizes local configurations over global ones.

Handle Missing Config Values

In this step, we will see what happens when you try to query a Git configuration value that doesn't exist and how Git handles this.

Sometimes you might try to query a setting that hasn't been configured yet, either globally or locally. Let's try to query a hypothetical setting called core.editor which we haven't explicitly set:

git config core.editor

Since we haven't set a local core.editor in our my-time-machine repository, Git will look for a global core.editor. If that's also not set (which is the case in this environment), Git will exit with an error and print nothing or a default value if one is configured system-wide. You might see output similar to this, or no output at all, depending on the system's default configuration:


The important thing is that Git doesn't find a specific value for core.editor in the local or global configuration files.

Now, let's try querying a setting that we know exists globally, but not locally, like user.email:

git config user.email

Even though we are in the my-time-machine directory where we set a local user.name, we did not set a local user.email. Git will first look for a local user.email. Since it doesn't find one, it will then look for a global user.email and return that value:

jane.doe@example.com

This confirms that when a local configuration is missing, Git falls back to the global configuration.

Understanding how Git searches for configuration values (local first, then global, then system) is crucial for troubleshooting and managing your Git environment effectively.

Summary

In this lab, we learned how to use the git config command to query Git settings. We started by querying specific global settings like user.name and user.email using the --global flag. This demonstrated how to retrieve individual configuration values that apply to all repositories on the system.

We then explored the different levels of Git configuration: system, global, and local, understanding that local settings override global ones. We learned how to list all global settings using git config --global --list to see the full configuration applied to our user account. This provided a comprehensive view of the default settings Git uses.