Git Config Global: How to Set Username, Email & Defaults (Step-by-Step)

Git Config Global: How to Set Username, Email & Defaults (Step-by-Step)

Quick Setup: Git Global Configuration

If you are setting up Git for the first time, use the following commands to configure your identity globally using git config. These settings will apply to all repositories on your system.

Set username and email

Run the following commands:

bash
git config --global user.name "Your Name"
git config --global user.email "your@email.com"

These values will be used in every commit you make with git commit.

Verify global configuration

To confirm your configuration:

bash
git config --list

You can also check specific values:

bash
git config user.name
git config user.email

What is git config --global?

The git config --global command is used to set configuration values that apply to all Git repositories for the current user.

Why global configuration is important

  • Avoids setting username and email for every repository
  • Ensures consistent commit identity
  • Required for making commits without errors using git commit
  • Simplifies initial Git setup

Where git global config is stored (~/.gitconfig)

Global configuration is stored in the following file:

bash
~/.gitconfig

You can view it using:

bash
cat ~/.gitconfig

Git Configuration Levels Explained

Global vs local vs system configuration

  • Local configuration (--local)

    • Applies only to a specific repository created using git init
    • Stored in .git/config
    • Highest priority
  • Global configuration (--global)

    • Applies to the current user
    • Stored in ~/.gitconfig
  • System configuration (--system)

    • Applies to all users on the system
    • Stored in /etc/gitconfig
    • Lowest priority

Order of precedence

When multiple configurations exist, Git follows this order:

  1. Local configuration (highest priority)
  2. Global configuration
  3. System configuration (lowest priority)

This means local settings override global settings, and global settings override system settings.


Common Git Global Config Commands

Set username

Set your name, which will appear in your commits:

bash
git config --global user.name "Your Name"

Set email

Set your email address used for commits:

bash
git config --global user.email "your@email.com"

Set default editor

Configure your preferred editor (for writing messages with git commit, etc.):

bash
git config --global core.editor vim

You can replace vim with editors like nano, code, or vi.

Set default branch name

Set the default branch name when initializing new repositories with git init:

bash
git config --global init.defaultBranch main

How to Check Git Configuration

List all configuration values

Display all Git configuration settings:

bash
git config --list

Get specific config values

Retrieve individual configuration values:

bash
git config user.name
git config user.email
git config core.editor

Git Config File Location in Linux

Git stores configuration settings in different files based on scope.

Global config file path

Global configuration is stored in:

bash
~/.gitconfig

View it using:

bash
cat ~/.gitconfig

System config file path

System-wide configuration is stored in:

bash
/etc/gitconfig

This requires root privileges to edit.

Repository config file path

Local repository configuration is stored in:

bash
.git/config

This applies only to the current repository.


First-Time Git Setup

Configure username and email

bash
git config --global user.name "Your Name"
git config --global user.email "your@email.com"

Set editor and default branch

bash
git config --global core.editor vim
git config --global init.defaultBranch main

Verify setup

bash
git config --list

Ensure all values are correctly set before starting to use Git repositories created using git init or git clone.


Common Issues with git config

Wrong username or email in commits

If commits created using git commit show incorrect details:

  • Update configuration using git config --global
  • Or set repository-specific values using --local

Configuration not applied

If settings are not applied:

  • Ensure you used --global or --local correctly
  • Restart terminal if needed
  • Verify using git config --list

Multiple config conflict

Conflicts occur when the same setting exists in multiple levels:

  • Local config overrides global
  • Global overrides system

To debug:

bash
git config --list --show-origin

This shows where each configuration value is defined.


Frequently Asked Questions

1. What is git config --global used for?

The git config --global command is used to set configuration values like username and email globally for all repositories on your system.

2. How do I set username and email in Git?

You can set them using git config --global user.name and git config --global user.email commands.

3. Where is git global config stored?

Git global configuration is stored in the ~/.gitconfig file.

4. What is the difference between global and local git config?

Global config applies to all repositories, while local config applies only to a specific repository.

5. How do I check my git configuration?

You can check configuration using git config --list or view the ~/.gitconfig file.

Summary

In this guide, you learned how to configure Git globally using the git config --global command.

  • Set username and email for commits
  • Configure editor and default branch
  • Understand configuration levels (local, global, system)
  • Verify and troubleshoot configuration issues

Proper Git configuration ensures consistent commit identity and a smooth development workflow when working with repositories created using git init.


Official Documentation

Deepak Prasad

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.