Configure Git
This guide explains how to set up and configure Git on your local machine. Git is a distributed version control system that allows you to track changes in your code, collaborate with others, and maintain different versions of your projects.
Install Git
- Install Git based on your operating system:
# For Ubuntu/Debian
sudo apt-get update
sudo apt-get install git
# For macOS (using Homebrew)
brew install git
# For Windows
# Download from https://git-scm.com/download/win
- Verify the installation:
Installing Git is the first step to using version control. The commands above will install the latest stable version for your operating system.
Configure User Identity
- Set your name and email globally:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
- Verify your configuration:
Your identity is used in commit messages to identify who made changes. Use the email associated with your GitHub, GitLab, or other Git hosting service.
Set Up Default Editor
- Configure your preferred text editor:
# For VSCode
git config --global core.editor "code --wait"
# For Vim
git config --global core.editor "vim"
# For Nano
git config --global core.editor "nano"
The default editor is used when Git needs you to enter commit messages or resolve conflicts.
Configure Line Endings
- Set appropriate line ending behavior:
# For macOS/Linux
git config --global core.autocrlf input
# For Windows
git config --global core.autocrlf true
Proper line ending configuration prevents issues when collaborating across different operating systems.
Set Up SSH for GitHub/GitLab
- Generate an SSH key pair:
ssh-keygen -t ed25519 -C "[email protected]"
- Start the SSH agent:
- Copy your public key:
# macOS
pbcopy < ~/.ssh/id_ed25519.pub
# Linux
cat ~/.ssh/id_ed25519.pub
# Then manually copy the output
# Windows (Git Bash)
cat ~/.ssh/id_ed25519.pub | clip
SSH keys provide secure authentication without requiring password entry each time you push or pull. Add the copied public key to your GitHub/GitLab account settings.
Configure Default Branch Name
- Set the default branch name for new repositories:
Many organizations have moved away from using "master" as the default branch name.
Set Up Useful Aliases
- Create shortcuts for common commands:
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
Aliases save time by reducing the need to type long commands. The last alias creates a visually appealing log output.
Verify Configuration
- View your complete configuration:
This command shows all your Git settings, helping you verify that everything is configured correctly.