Git Installation and Setup โ
Git is a distributed version control system that helps you track changes in your code, collaborate with others, and manage different versions of your projects. This tutorial will guide you through installing Git on different operating systems and setting up your initial configuration.
What is Git? โ
Git is a powerful version control system that:
- Tracks changes in your files over time
- Allows you to revert to previous versions
- Enables collaboration with multiple developers
- Manages different versions (branches) of your project
- Works offline and synchronizes when connected
Installing Git โ
Windows โ
Option 1: Official Git for Windows โ
- Visit git-scm.com
- Download the latest version for Windows
- Run the installer and follow these recommended settings:
- Choose "Use Git from the Windows Command Prompt"
- Select "Checkout Windows-style, commit Unix-style line endings"
- Choose "Use Windows' default console window"
Option 2: Using Package Manager (Chocolatey) โ
If you have Chocolatey installed:
choco install git
Option 3: Using Package Manager (Scoop) โ
If you have Scoop installed:
scoop install git
macOS โ
Option 1: Using Homebrew (Recommended) โ
brew install git
Option 2: Using MacPorts โ
sudo port install git
Option 3: Xcode Command Line Tools โ
xcode-select --install
Option 4: Official Installer โ
- Visit git-scm.com
- Download the macOS installer
- Run the installer and follow the instructions
Linux โ
Ubuntu/Debian โ
sudo apt update
sudo apt install git
CentOS/RHEL/Fedora โ
# CentOS/RHEL
sudo yum install git
# Fedora
sudo dnf install git
Arch Linux โ
sudo pacman -S git
OpenSUSE โ
sudo zypper install git
Verifying Installation โ
After installation, verify Git is installed correctly:
git --version
You should see output similar to:
git version 2.39.0
Initial Git Configuration โ
Before using Git, you need to configure your identity. This information will be attached to your commits.
Setting Your Identity โ
Configure your name and email address:
git config --global user.name "Your Full Name"
git config --global user.email "[email protected]"
Example:
git config --global user.name "John Doe"
git config --global user.email "[email protected]"
Setting Your Default Editor โ
Configure your preferred text editor for Git operations:
# For Visual Studio Code
git config --global core.editor "code --wait"
# For Vim
git config --global core.editor "vim"
# For Nano
git config --global core.editor "nano"
# For Sublime Text
git config --global core.editor "subl -n -w"
Setting Default Branch Name โ
Set the default branch name for new repositories:
git config --global init.defaultBranch main
Configuring Line Endings โ
Windows โ
git config --global core.autocrlf true
macOS/Linux โ
git config --global core.autocrlf input
Advanced Configuration Options โ
Setting Up Aliases โ
Create shortcuts for common Git 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.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual '!gitk'
Configuring Push Behavior โ
Set the default push behavior:
git config --global push.default simple
Setting Up Credential Storage โ
To avoid typing your password repeatedly:
Windows โ
git config --global credential.helper manager-core
macOS โ
git config --global credential.helper osxkeychain
Linux โ
git config --global credential.helper store
Viewing Your Configuration โ
To see all your Git configuration settings:
git config --list
To see a specific configuration value:
git config user.name
git config user.email
To see where a setting is defined:
git config --show-origin user.name
Configuration File Locations โ
Git configuration is stored in three levels:
- System-wide:
/etc/gitconfig
(affects all users) - User-specific:
~/.gitconfig
or~/.config/git/config
(affects current user) - Repository-specific:
.git/config
(affects current repository only)
Each level overrides the previous one, so repository-specific settings take precedence.
SSH Key Setup (Optional but Recommended) โ
For secure authentication with remote repositories like GitHub:
Generate SSH Key โ
ssh-keygen -t ed25519 -C "[email protected]"
Add SSH Key to SSH Agent โ
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Copy Public Key โ
# macOS
pbcopy < ~/.ssh/id_ed25519.pub
# Linux
xclip -sel clip < ~/.ssh/id_ed25519.pub
# Windows
clip < ~/.ssh/id_ed25519.pub
Then add this public key to your GitHub/GitLab/Bitbucket account.
Troubleshooting Common Issues โ
Permission Denied Error โ
If you encounter permission issues:
sudo chown -R $(whoami) ~/.gitconfig
HTTPS vs SSH โ
If you're having authentication issues, you might need to switch between HTTPS and SSH:
# Check current remote URL
git remote -v
# Change to SSH
git remote set-url origin [email protected]:username/repository.git
# Change to HTTPS
git remote set-url origin https://github.com/username/repository.git
Certificate Issues โ
If you encounter SSL certificate errors:
git config --global http.sslVerify false
Note: Only use this as a temporary solution and re-enable SSL verification afterward.
Next Steps โ
Now that you have Git installed and configured, you're ready to:
- Create your first Git repository
- Learn basic Git commands
- Start tracking changes in your projects
- Collaborate with others using Git
Summary โ
In this tutorial, you learned how to:
- Install Git on Windows, macOS, and Linux
- Configure your Git identity and preferences
- Set up SSH keys for secure authentication
- Troubleshoot common installation issues
- Understand Git configuration hierarchy
Git is now ready to help you track changes, collaborate with others, and manage your code effectively. In the next tutorial, we'll explore Git basics and terminology to build your foundation.