Git Installation and Configuration
This chapter will detail how to install Git on different operating systems and perform basic configuration, allowing you to quickly start using Git.
Git Installation
Windows System Installation
Method 1: Official Installer
- Visit the Git official website
- Click "Download for Windows"
- Run the installer after downloading
- Follow the installation wizard to complete installation
bash
# Verify installation
git --versionMethod 2: Using Package Managers
bash
# Using Chocolatey
choco install git
# Using Scoop
scoop install git
# Using winget
winget install Git.GitInstallation Options Explanation
- Adjust PATH environment: Choose "Git from the command line and also from 3rd-party software"
- Choose HTTPS transport backend: Choose "Use the OpenSSL library"
- Configure line ending conversion: Choose "Checkout Windows-style, commit Unix-style line endings"
- Configure terminal emulator: Choose "Use Windows' default console window"
macOS System Installation
Method 1: Using Homebrew (Recommended)
bash
# Install Homebrew (if not already installed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install Git
brew install git
# Verify installation
git --versionMethod 2: Using MacPorts
bash
sudo port install gitMethod 3: Official Installer
- Visit the Git official website
- Download macOS version
- Run the .dmg file and follow prompts to install
Method 4: Xcode Command Line Tools
bash
# Install Xcode command line tools (includes Git)
xcode-select --installLinux System Installation
Ubuntu/Debian System
bash
# Update package list
sudo apt update
# Install Git
sudo apt install git
# Verify installation
git --versionCentOS/RHEL/Fedora System
bash
# CentOS/RHEL 7
sudo yum install git
# CentOS/RHEL 8+ or Fedora
sudo dnf install git
# Verify installation
git --versionArch Linux
bash
# Install Git
sudo pacman -S git
# Verify installation
git --versionCompile from Source
bash
# Install dependencies
sudo apt install make libssl-dev libghc-zlib-dev libcurl4-gnutls-dev libreadline-dev autoconf build-essential gettext libtool
# Download source code
wget https://github.com/git/git/archive/v2.40.0.tar.gz
tar -xzf v2.40.0.tar.gz
cd git-2.40.0
# Compile and install
make configure
./configure --prefix=/usr/local
make all
sudo make install
# Verify installation
git --versionGit Basic Configuration
User Information Configuration
After installation, you first need to configure user information:
bash
# Configure username
git config --global user.name "Your Name"
# Configure email
git config --global user.email "your.email@example.com"
# View configuration
git config --global user.name
git config --global user.emailConfiguration Levels
Git has three configuration levels:
bash
# System-level configuration (all users)
git config --system user.name "System User"
# Global configuration (current user)
git config --global user.name "Global User"
# Repository-level configuration (current repository)
git config --local user.name "Local User"Priority: local > global > system
Viewing Configuration
bash
# View all configuration
git config --list
# View specific configuration
git config user.name
git config user.email
# View configuration source
git config --list --show-origin
# View global configuration
git config --global --list
# View local configuration
git config --local --listEditor Configuration
bash
# Set default editor to VS Code
git config --global core.editor "code --wait"
# Set to Vim
git config --global core.editor vim
# Set to Nano
git config --global core.editor nano
# Set to Notepad++ (Windows)
git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"Diff Tool Configuration
bash
# Set diff viewing tool
git config --global diff.tool vimdiff
# Set to VS Code
git config --global diff.tool vscode
git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'
# Set merge tool
git config --global merge.tool vimdiffAlias Configuration
Create aliases for commonly used commands:
bash
# Basic aliases
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
# Advanced aliases
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual '!gitk'
# Beautified log
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
# View aliases
git config --get-regexp aliasLine Ending Configuration
bash
# Windows system recommended configuration
git config --global core.autocrlf true
# macOS/Linux system recommended configuration
git config --global core.autocrlf input
# Disable automatic conversion
git config --global core.autocrlf false
# Set safety check
git config --global core.safecrlf trueColor Configuration
bash
# Enable color output
git config --global color.ui auto
# Specific configuration
git config --global color.branch auto
git config --global color.diff auto
git config --global color.status auto
# Custom colors
git config --global color.diff.meta "blue black bold"Push Configuration
bash
# Set push strategy (recommended)
git config --global push.default simple
# Other push strategies
git config --global push.default current # Push current branch to remote branch with same name
git config --global push.default upstream # Push to upstream branch
git config --global push.default matching # Push all matching branchesCredential Configuration
bash
# Windows credential manager
git config --global credential.helper manager-core
# macOS keychain
git config --global credential.helper osxkeychain
# Linux cache credentials
git config --global credential.helper cache
# Set cache time (seconds)
git config --global credential.helper 'cache --timeout=3600'SSH Key Configuration
Generating SSH Keys
bash
# Generate new SSH key
ssh-keygen -t ed25519 -C "your.email@example.com"
# If system doesn't support ed25519, use RSA
ssh-keygen -t rsa -b 4096 -C "your.email@example.com"
# Start SSH agent
eval "$(ssh-agent -s)"
# Add key to SSH agent
ssh-add ~/.ssh/id_ed25519Configuring SSH Keys
bash
# View public key content
cat ~/.ssh/id_ed25519.pub
# Copy public key to clipboard (macOS)
pbcopy < ~/.ssh/id_ed25519.pub
# Copy public key to clipboard (Linux)
xclip -sel clip < ~/.ssh/id_ed25519.pub
# Copy public key to clipboard (Windows Git Bash)
clip < ~/.ssh/id_ed25519.pubTesting SSH Connection
bash
# Test GitHub connection
ssh -T git@github.com
# Test GitLab connection
ssh -T git@gitlab.com
# Test Bitbucket connection
ssh -T git@bitbucket.orgConfiguration File Locations
Configuration File Paths
bash
# System-level configuration file
# Windows: C:\Program Files\Git\etc\gitconfig
# macOS/Linux: /etc/gitconfig
# Global configuration file
# Windows: C:\Users\<username>\.gitconfig
# macOS/Linux: ~/.gitconfig
# Repository-level configuration file
# Project root directory/.git/configEditing Configuration Files Directly
bash
# Edit global configuration file
git config --global --edit
# Edit local configuration file
git config --editExample .gitconfig file:
ini
[user]
name = Zhang San
email = zhangsan@example.com
[core]
editor = code --wait
autocrlf = true
safecrlf = true
[color]
ui = auto
[alias]
co = checkout
br = branch
ci = commit
st = status
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
[push]
default = simple
[credential]
helper = manager-core
[diff]
tool = vscode
[difftool "vscode"]
cmd = code --wait --diff $LOCAL $REMOTE
[merge]
tool = vscode
[mergetool "vscode"]
cmd = code --wait $MERGEDProxy Configuration
HTTP/HTTPS Proxy
bash
# Set HTTP proxy
git config --global http.proxy http://proxy.company.com:8080
# Set HTTPS proxy
git config --global https.proxy https://proxy.company.com:8080
# Set SOCKS proxy
git config --global http.proxy socks5://127.0.0.1:1080
# Remove proxy settings
git config --global --unset http.proxy
git config --global --unset https.proxyDomain-Specific Proxy
bash
# Set proxy for GitHub
git config --global http.https://github.com.proxy socks5://127.0.0.1:1080
# Remove specific domain proxy
git config --global --unset http.https://github.com.proxyCommon Configuration Issues
Issue 1: Chinese Filename Display
bash
# Fix issue where Chinese filenames display as numbers
git config --global core.quotepath falseIssue 2: Line Ending Issues
bash
# View current settings
git config core.autocrlf
# Windows user settings
git config --global core.autocrlf true
# macOS/Linux user settings
git config --global core.autocrlf inputIssue 3: Case Sensitivity
bash
# Set case sensitivity (default false)
git config --global core.ignorecase falseIssue 4: File Permission Issues
bash
# Ignore file permission changes
git config --global core.filemode falseVerification Configuration
Create a test script to verify configuration:
bash
#!/bin/bash
echo "=== Git Configuration Verification ==="
echo "Git version: $(git --version)"
echo "Username: $(git config user.name)"
echo "Email: $(git config user.email)"
echo "Editor: $(git config core.editor)"
echo "Line ending handling: $(git config core.autocrlf)"
echo "Color output: $(git config color.ui)"
echo "Push strategy: $(git config push.default)"
echo ""
echo "=== SSH Key Test ==="
if [ -f ~/.ssh/id_ed25519.pub ]; then
echo "SSH key exists: ~/.ssh/id_ed25519.pub"
else
echo "SSH key does not exist, please generate a key"
fi
echo ""
echo "=== Alias Configuration ==="
git config --get-regexp aliasSummary
Proper Git installation and configuration is the foundation for efficient Git usage:
Installation Key Points
- ✅ Choose installation method suitable for your operating system
- ✅ Verify that installation was successful
- ✅ Understand meaning of different installation options
Configuration Key Points
- ✅ Set username and email (required)
- ✅ Configure appropriate editor
- ✅ Set line ending handling strategy
- ✅ Create useful aliases
- ✅ Configure SSH keys
Best Practices
- 🔧 Use global configuration for general options
- 🔧 Set specific local configurations for different projects
- 🔧 Regularly backup configuration files
- 🔧 Understand configuration priority
After completing configuration, you can start using Git for version control. In the next chapter, we will learn Git's quick start guide.