Git Installation and Configuration
Installing Git is the first step to using it. This chapter will guide you through installing Git on various operating systems and performing basic configuration.
1. What You'll Learn
- Install Git on Windows, Linux, and macOS
- Set up your username and email address
- Set the default editor
- Verify that the installation was successful
- Basic Git Configuration Options
2. Installing Git on Windows
(1) 2.1 Method 1: Using the Official Installer (Recommended)
Steps:
- Visit the Git website: https://git-scm.com/download/win
- Download the latest installation package (approximately 50 MB)
- Run the installer and follow the wizard to complete the installation
Options During Installation:
| Option | Recommended Setting | Description |
|---|---|---|
| Component Selection | Select All | Includes Git Bash, GUI, and associated files |
| Default Editor | VS Code | Choose an editor you're familiar with |
| PATH Environment | Git from the Command Line | Can be used in CMD/PowerShell |
| SSH Executable | Use OpenSSH | Use Git's Built-in SSH |
| HTTPS backend | Use the OpenSSL library | More stable |
| Line-End Conversion | Checkout Windows-style, commit Unix-style | Recommended for Cross-Platform Collaboration |
| Terminal Emulator | Use MinTTY | Git Bash Default Terminal |
▶ Example: Verifying the Git Installation
# InspectionGitVersion
git --version
# ViewGitInstallation Path
where git
# ViewGitLayout
git config --list
(2) 2.2 Method 2: Using a Package Manager
Using Chocolatey:
choco install git
Using Scoop:
scoop install git
(3) 2.3 Verifying the Installation
Open Git Bash or PowerShell, and type:
git --version
The output looks something like this:
git version 2.43.0
3. Installing Git on Linux
(1) 3.1 Installation Commands for Each Distribution
Debian/Ubuntu:
sudo apt update
sudo apt install git
Fedora:
sudo dnf install git
CentOS/RHEL:
sudo yum install git
Arch Linux:
sudo pacman -S git
openSUSE:
sudo zypper install git
(2) 3.2 Compiling from Source (To Get the Latest Version)
# Install Dependencies
sudo apt install libcurl4-openssl-dev libexpat1-dev libssl-dev zlib1g-dev
# Download the source code
wget https://github.com/git/git/archive/refs/tags/v2.43.0.tar.gz
tar -xzf v2.43.0.tar.gz
cd git-2.43.0
# Compile and Install
make prefix=/usr/local all
sudo make prefix=/usr/local install
(3) 3.3 Verifying the Installation
git --version
The output looks something like this:
git version 2.43.0
4. Installing Git on macOS
(1) 4.1 Method 1: Using the Xcode Command Line Tools (Recommended)
macOS comes with Git pre-installed, but it may not be the latest version. Installing the Xcode Command Line Tools will update Git:
xcode-select --install
An installation dialog box will pop up; just click "Install."
(2) 4.2 Method 2: Using Homebrew
brew install git
(3) 4.3 Method 3: Using the Official Installation Package
- Visit: https://git-scm.com/download/mac
- Download the DMG file
- Double-click to install
(4) 4.4 Verify the Installation
git --version
The output looks something like this:
git version 2.43.0
5. Basic Git Configuration
After installing Git, you'll need to perform some basic configuration. Git configuration is divided into three levels:
| Level | File Location | Scope | Priority |
|---|---|---|---|
| System-wide | /etc/gitconfig |
All users | Low |
| Global | ~/.gitconfig |
Current User | Medium |
| Local Level | .git/config |
Current Repository | High |
(1) 5.1 Configure Username and Email Address (Required)
Git needs to know who committed the code, so you must configure your username and email address:
# Configure Global Username
git config --global user.name "Zhang San"
# Configure a Global Email Account
git config --global user.email "zhangsan@example.com"
Note: Your email address will be used to link your GitHub/GitLab account, so we recommend using a valid email address.
(2) 5.2 Configuring the Default Editor
Git requires you to enter a commit message when making a commit; by default, it uses the system editor.
Configuring VS Code:
git config --global core.editor "code --wait"
Configuring Vim:
git config --global core.editor "vim"
Configuring Nano:
git config --global core.editor "nano"
(3) 5.3 Configuring the Default Branch Name
Git 2.28 and later support configuring the default branch name:
git config --global init.defaultBranch main
(4) 5.4 Configuring Line Break Conversion (Cross-Platform Collaboration)
Windows Users:
# Convert to upon detectionCRLF,Convert upon submission toLF
git config --global core.autocrlf true
Linux/macOS users:
# Do not convert when detected,Convert upon submission toLF
git config --global core.autocrlf input
(5) 5.5 Configuring Aliases (for Efficiency)
Set short aliases for commonly used commands:
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.lg "log --oneline --graph --all"
Using aliases:
git st # equivalent to git status
git co main # equivalent to git checkout main
git lg # equivalent to git log --oneline --graph --all
6. View Configuration
(1) 6.1 View All Configurations
git config --list
Output example:
user.name=Zhang San
user.email=zhangsan@example.com
core.editor=code --wait
init.defaultbranch=main
core.autocrlf=true
(2) 6.2 Viewing Specific Configuration Items
git config user.name
Output:
Zhang San
(3) 6.3 Viewing the Location of Configuration Files
# View System-Level Configuration
git config --system --list
# View Global-Level Configuration
git config --global --list
# View Local-Level Configuration
git config --local --list
7. Git GUI Tools
In addition to the command line, Git also offers graphical tools:
(1) 7.1 Git's Built-in GUI
git gui # Git GUI
gitk # GitHistory Viewer
(2) 7.2 Third-Party GUI Tools
| Tool | Platform | Features |
|---|---|---|
| SourceTree | Windows/Mac | Free, full-featured |
| GitKraken | Windows/Mac/Linux | Attractive interface, paid |
| GitHub Desktop | Windows/Mac | Simple and easy to use, free |
| TortoiseGit | Windows | Integrated with File Explorer |
| VS Code Git | Cross-platform | Built-in Git support |
8. SSH Key Configuration (Optional but Recommended)
When communicating with GitHub or GitLab, SSH is more convenient than HTTPS (since you don't have to enter your password every time).
(1) 8.1 Generating SSH Keys
ssh-keygen -t ed25519 -C "zhangsan@example.com"
Press Enter three times (use the default path; do not set a password).
(2) 8.2 Viewing the Public Key
cat ~/.ssh/id_ed25519.pub
(3) 8.3 Adding to GitHub
- Copy the public key
- Login to GitHub → Settings → SSH and GPG keys → New SSH key
- Paste the public key and save it
(4) 8.4 Testing the Connection
ssh -T git@github.com
Successful output:
Hi zhangsan! You've successfully authenticated, but GitHub does not provide shell access.
❓ FAQ
git config --global user.name "new name"--unset option: git config --global --unset user.name📖 Summary
- Git supports the three major platforms: Windows, Linux, and macOS
- On Windows, we recommend using the official installer or a package manager to install it.
- Install using the package manager for each Linux distribution
- Install on macOS using the Xcode Command Line Tools or Homebrew
- You must configure a username and email address:
git config --global user.name/email - Configurable editor, default branch names, line break conversion, aliases, and more
- Once SSH keys are configured, you can communicate with GitHub/GitLab without entering a password.
- There are several GUI tools available for Git
📝 Exercises
-
Basic Exercise: Install Git on your computer, configure your username and email address, and use
git --versionandgit config --listto verify the installation and configuration. -
Advanced Exercise: Configure Git aliases; set up aliases for at least three commonly used commands, and execute these commands using the aliases.
-
Challenge: Generate an SSH key, add it to your GitHub account, and test whether the SSH connection works.



