How to know the git username and email saved during configuration?

Learn how to know the git username and email saved during configuration? with practical examples, diagrams, and best practices. Covers git, git-config development techniques with visual explanations.

How to Check Your Git Username and Email Configuration

Hero image for How to know the git username and email saved during configuration?

Learn how to quickly retrieve the username and email address Git uses for your commits, both globally and for specific repositories.

Git uses a username and email address to identify the author of each commit. This information is crucial for tracking contributions, especially in collaborative projects. If you're working on multiple projects or machines, you might need to verify which credentials Git is currently configured to use. This article will guide you through the commands to check your Git username and email, covering both global and local configurations.

Understanding Git Configuration Levels

Git stores configuration settings at different levels, allowing for flexibility:

  • System Level: Applies to all users on the system and all repositories. This is typically found in /etc/gitconfig.
  • Global Level: Applies to the current user across all their repositories. This is stored in ~/.gitconfig or ~/.config/git/config.
  • Local Level: Applies only to the specific repository you are currently working in. This is located in the .git/config file within the repository's directory.

When Git needs a configuration value, it checks these levels in order: local, then global, then system. The most specific setting (local) overrides the more general ones.

flowchart TD
    A[Start: Git needs config value] --> B{Check Local Config (.git/config)}
    B -- Found --> C[Use Local Value]
    B -- Not Found --> D{Check Global Config (~/.gitconfig)}
    D -- Found --> E[Use Global Value]
    D -- Not Found --> F{Check System Config (/etc/gitconfig)}
    F -- Found --> G[Use System Value]
    F -- Not Found --> H[No Value Found / Default]
    C --> I[End]
    E --> I[End]
    G --> I[End]
    H --> I[End]

Git Configuration Lookup Hierarchy

Checking Global Git Username and Email

The global configuration is the most common place to set your identity, as it applies to most of your projects. To check your globally configured username and email, use the git config command with the --global flag.

git config --global user.name
git config --global user.email

Retrieve global Git username and email

These commands will output your globally set username and email respectively. If nothing is returned, it means these values have not been set at the global level.

Checking Local Git Username and Email

Sometimes, you might need to use a different identity for a specific project (e.g., a work email for a company project and a personal email for an open-source contribution). In such cases, you set the user.name and user.email locally within that repository. To check these local settings, navigate into the repository directory and run the git config command without the --global flag.

cd /path/to/your/repository
git config user.name
git config user.email

Retrieve local Git username and email for the current repository

Similar to the global check, if these commands return no output, it means the values are not set locally. In this scenario, Git will fall back to the global configuration (if set).

Viewing All Configured Values

To get a comprehensive list of all Git configuration settings that apply to your current repository, including local, global, and system values, you can use the --list flag. This is useful for debugging or understanding the full context of your Git setup.

git config --list

List all Git configuration settings

The output will show all settings, with local settings overriding global ones, and global settings overriding system ones. You'll see entries like user.name=Your Name and user.email=your.email@example.com among others.