Compare Global vs Local Config
Git has different levels of configuration. We've already seen the --global
configuration, which applies to all your repositories. There's also a --local
configuration, which applies only to the specific repository you are currently in. Local configurations override global configurations for that particular repository.
To see the local configuration for a repository, you need to be inside a Git repository and run git config --local --list
. Since we haven't created a new repository in this lab yet, running this command now will result in an error.
Let's first create a new directory and initialize a Git repository inside it, just like you did in the "Your First Git Lab":
mkdir my-local-repo
cd my-local-repo
git init
You should see the initialization message:
Initialized empty Git repository in /home/labex/project/my-local-repo/.git/
Now that you are inside the my-local-repo
directory, which is a Git repository, you can check its local configuration:
git config --local --list
You will likely see no output, or very minimal output, because no local configurations have been set yet for this specific repository.
Now, let's set a local configuration that is different from the global one. For example, let's set a different user.name
for this specific repository:
git config --local user.name "Local User"
This command sets the user.name
specifically for the my-local-repo
.
Now, list the local configuration again:
git config --local --list
You should now see the local user.name
:
user.name=Local User
Finally, let's see what happens when you ask for the user.name
without specifying --global
or --local
. Git will look for the configuration in this order: local, global, and then system. The first one it finds will be used.
Run this command while still inside my-local-repo
:
git config user.name
You should see the local user name, because the local configuration overrides the global one:
Local User
Now, navigate back to the ~/project
directory (outside of the my-local-repo
):
cd ~/project
And run the same command again:
git config user.name
This time, since you are not inside a repository with a local configuration, Git will use the global configuration:
Jane Doe
This demonstrates how local configurations take precedence over global configurations, allowing you to have specific settings for individual projects while maintaining general settings for all others.