Setting your Git username and email is essential for tracking commits and collaborating on projects. This guide shows you how to configure Git credentials globally for all repositories or locally for specific projects.
Why Set Git Username and Email?
Your Git username and email are included in every commit you make. They help:
- Identify who made specific changes in the commit history
- Enable proper attribution in collaborative projects
- Connect commits to your GitHub/GitLab profile
- Maintain accountability in team environments
Set Git Config Globally (All Repositories)
Use global configuration to set your credentials for all repositories on your system:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Example:
git config --global user.name "John Doe"
git config --global user.email "[email protected]"
The --global flag saves these settings to ~/.gitconfig, making them the default for all Git repositories on your machine.
Set Git Config Locally (Specific Repository)
To use different credentials for a specific project, navigate to the repository directory and run:
cd /path/to/your/repository
git config user.name "Your Name"
git config user.email "[email protected]"
Example:
cd ~/projects/work-project
git config user.name "John Doe"
git config user.email "[email protected]"
Local settings are stored in .git/config within the repository and override global settings.
When to Use Global vs Local Config
Use Global Config when:
- You work on personal projects with consistent identity
- You want default credentials for all repositories
- You're a solo developer
Use Local Config when:
- You have multiple GitHub/GitLab accounts (personal and work)
- Different projects require different email addresses
- You contribute to open-source with specific credentials
Verify Your Git Configuration
Check your current Git settings:
# View all configurations
git config --list
# Check specific global setting
git config --global user.name
git config --global user.email
# Check specific local setting (in repository directory)
git config user.name
git config user.email
See where settings are defined:
git config --list --show-origin
This shows which config file each setting comes from.
Edit Git Config File Directly
You can manually edit configuration files if needed.
Global config file:
nano ~/.gitconfig
Add or modify:
[user]
name = Your Name
email = [email protected]
Local config file:
nano .git/config
Add or modify the same [user] section.
Configuration Priority
Git follows this priority order (highest to lowest):
- Local - Repository-specific (
.git/config) - Global - User-specific (
~/.gitconfig) - System - System-wide (
/etc/gitconfig)
Local settings always override global settings for that repository.
Common Use Cases
Personal and Work Accounts:
# Set global (personal) credentials
git config --global user.name "Jane Smith"
git config --global user.email "[email protected]"
# Set local (work) credentials for work project
cd ~/work/company-repo
git config user.name "Jane Smith"
git config user.email "[email protected]"
Multiple GitHub Accounts:
# Personal projects use global config
# Work projects use local config in each repo
cd ~/work/project
git config user.email "[email protected]"
Remove or Change Git Config
Change existing configuration:
# Update global
git config --global user.email "[email protected]"
# Update local
git config user.email "[email protected]"
Remove configuration:
# Remove global setting
git config --global --unset user.name
# Remove local setting
git config --unset user.name
Troubleshooting
Commits showing wrong author:
Check your configuration and ensure local settings override correctly:
git config user.name
git config user.email
Email privacy on GitHub:
Use GitHub's no-reply email for privacy:
git config --global user.email "[email protected]"
Find your GitHub no-reply email in Settings → Emails.
Quick Command Reference
# Set global username and email
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
# Set local username and email
git config user.name "Your Name"
git config user.email "[email protected]"
# View all configurations
git config --list
# Check specific setting
git config user.name
# View config file locations
git config --list --show-origin
# Remove setting
git config --unset user.name
Best Practices
- Always set your Git username and email before making commits
- Use your real name and valid email address
- Set local config for work repositories to separate personal and professional commits
- Verify your configuration with
git config --listafter setup - Use GitHub's no-reply email if you want to keep your email private
External Resources
- Git Config Documentation - Official Git configuration reference
- Git Basics - First-Time Setup - Pro Git book chapter
- GitHub Email Settings - Configure GitHub email preferences
- GitLab User Settings - GitLab profile configuration
Add new comment