Introduction
Git stores configuration settings at three levels: system, global, and local (repository-specific). The interplay between these configuration levels allows for powerful customization and control of Git behavior both across your entire system as well as on a repo-by-repo basis.
Understanding where your global Git configuration file is stored and how to access it is key to effectively leveraging this functionality. This guide will dive deep on the global Git config – how it works, its location on various systems, and best practices for managing configurations at scale.
Global Git Config Details
The global Git configuration file applies settings to all Git repositories and operations performed by a user on their system. It allows customizing Git without needing access to update system-level configs or having to tweak settings on every individual repo you work with.
Technical Implementation
Under the hood, Git stores configurations as simple key-value pairs in plain text files. The global config resides in a file pointed to by the $GIT_CONFIG
environment variable, falling back to ~/.gitconfig
by default on most operating systems if unset [1].
For example, the user.name
setting gets stored as:
[user]
name = John Doe
Internally, Git pulls configuration data from files in a defined order – repository .git/config
, user-level (global)
, then system $(prefix)/etc/gitconfig
. Settings deeper in this hierarchy take precedence [2].
So if user.name
was set both globally as above and locally in .git/config
within a repo, the repo-specific value would be used. These configurable "levels" allow git to be flexibly adapted.
Setting vs Getting Configs
Git provides command line interfaces for both setting and getting configured values [3]:
# Set globally
git config --global user.name "John Doe"
# Get globally
git config --global user.name
Settings can also be set manually by editing the global .gitconfig
file directly.
Global Config File Location
The specific location of the global .gitconfig
file depends on operating system:
Windows
C:\Users\<username>\.gitconfig
Stores global settings directly in the user‘s home folder.
Linux & Unix-likes
/home/<username>/.gitconfig
Stored in user‘s $HOME directory on most Linux distros and Unix systems like macOS or FreeBSD if unset.
Some exceptions include:
- Ubuntu:
/home/<username>/.config/git/config
- OpenSUSE:
/home/<username>/.config/git/config
Determining Actual Location
Use git config --list --show-origin
to confirm the actual file path for the global config on your particular system.
Viewing & Editing Global Config
You can directly view or edit your global .gitconfig
file at the path above in your text editor of choice.
For quick viewing, you can also output its contents to terminal:
git config --global --list
Common settings to customize include [4]:
user.name
: The name attached to commitsuser.email
: Email attached to commitscore.editor
: Default text editor to usecolor.ui
: Colorization of git outputalias.*
: Shorthand custom commands
For example:
[user]
name = John Doe
email = jdoe@company.com
[color]
ui = true
[alias]
co = checkout
cm = commit
Global vs Local vs System Config
The global, local, and system levels balance tradeoffs in terms of scope and precedence [5].
Local Config
The local config only affects a specific Git repository. This allows teams to have project-specific settings like templates or hooks for a standardized workflow. Local takes highest precedence.
Global Config
Global affects all repos for a user. This is most appropriate for user-specific defaults like preferred text editor. Global overrides system but is overridden by local settings.
System Config
The system level config, located at /etc/gitconfig
on Linux systems, sets system-wide defaults for all users. This is lowest precedence, generally used by administrators.
Config Level | Scope | Location | Precedence |
---|---|---|---|
Local | Single repository | .git/config | Highest |
Global | Per-user | ~/.gitconfig | Middle |
System | All users | /etc/gitconfig | Lowest |
So order is:
- Local repository config
- Global user-level config
- System-wide config
Best Practices
Some best practices when managing Git configurations include:
Conventions
Agree on organizational naming conventions for custom config properties. For example, prefixing with company identifier:
acme.build.directory=/opt/acme/build
Security
Don‘t commit private keys, access tokens and other secrets. Instead pass these as needed through OS environment variables or credential storage.
DevOps Integration
Tools like Ansible, Puppet, and Chef allow automating management of Git configs at scale across systems.
Configuration Management
Larger enterprises may enforce policy via a central repository with pre-defined config templates managed by IT. Developers then replicate centrally managed setttings.
Conclusion
The global Git configuration at ~/.gitconfig
provides powerful user-level customization that automagically applies to all your repositories. Understanding how to access and edit the global config unlocks greater efficiency – allowing you to configure your personalized Git workflows and defaults.
By layering system, global, and local settings – Git allows simple yet very granular configurations tuned precisely for particular operating systems, users, teams or individual repos. Following best practices around organization, security and configuration management assists managing this complexity at enterprise scale.
So now that you know precisely where your Git global config file resides go forth and harness its capabilities for your project and setup needs!
References
- Pro Git Book – Git Config: https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration
- Atlassian Docs – Git Config Levels: https://www.atlassian.com/git/tutorials/setting-up-a-repository/git-config
- Git SCM – git-config Documentation: https://git-scm.com/docs/git-config
- GitHub – Git Config Tutorial: https://github.blog/2015-08-19-git-config-tutorials/
- Git Tower Docs – Git Config Scopes: https://www.git-tower.com/learn/git/commands/git-config#scopes