0
0
Gitdevops~5 mins

Global vs local configuration in Git - CLI Comparison

Choose your learning style9 modes available
Introduction
Git lets you set configuration options that control how it works. These settings can be saved globally for all projects or locally for just one project. This helps you customize Git behavior depending on your needs.
When you want your name and email to be the same for all your Git projects on your computer
When you need a different username or email for a specific project, like work vs personal
When you want to change Git settings like line endings or merge tools only for one project
When you want to check what settings are active in your current project
When you want to fix a problem caused by wrong Git settings in one project without affecting others
Commands
This sets your name globally for all Git projects on your computer. Git uses this name in commits unless overridden locally.
Terminal
git config --global user.name "Alice Smith"
Expected OutputExpected
No output (command runs silently)
--global - Sets the configuration for all projects for the current user
This sets your email globally for all Git projects. It is used in commits to identify the author.
Terminal
git config --global user.email "alice@example.com"
Expected OutputExpected
No output (command runs silently)
--global - Applies the setting to all repositories for the user
This sets your name only for the current Git project. It overrides the global name for this project.
Terminal
git config --local user.name "Alice Work"
Expected OutputExpected
No output (command runs silently)
--local - Sets the configuration only for the current repository
Shows all Git configuration settings currently active in the current project, including global and local ones.
Terminal
git config --list
Expected OutputExpected
user.name=Alice Work user.email=alice@example.com
Removes the local user.name setting, so Git will use the global name again in this project.
Terminal
git config --local --unset user.name
Expected OutputExpected
No output (command runs silently)
--unset - Removes a configuration setting
--local - Targets the local repository configuration
Key Concept

Local Git settings override global settings for the current project only.

Common Mistakes
Setting user.name without --global or --local flags
Git may set the value in an unexpected config file or fail silently, causing confusion about which setting is active.
Always specify --global to set for all projects or --local to set for the current project explicitly.
Assuming global settings apply when local settings exist
Local settings take priority, so global settings are ignored in that project, leading to unexpected author info in commits.
Check local settings with git config --list and remove local overrides if you want global settings to apply.
Summary
Use git config --global to set options for all your Git projects on your computer.
Use git config --local to set options only for the current project, overriding global settings.
Check active settings with git config --list to see which values Git uses in the current project.