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


2. Installing Git on Windows

Steps:

  1. Visit the Git website: https://git-scm.com/download/win
  2. Download the latest installation package (approximately 50 MB)
  3. 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

BASH
# InspectionGitVersion
git --version

# ViewGitInstallation Path
where git

# ViewGitLayout
git config --list

(2) 2.2 Method 2: Using a Package Manager

Using Chocolatey:

POWERSHELL
choco install git

Using Scoop:

POWERSHELL
scoop install git

(3) 2.3 Verifying the Installation

Open Git Bash or PowerShell, and type:

BASH
git --version

The output looks something like this:

TEXT
git version 2.43.0

3. Installing Git on Linux

(1) 3.1 Installation Commands for Each Distribution

Debian/Ubuntu:

BASH
sudo apt update
sudo apt install git

Fedora:

BASH
sudo dnf install git

CentOS/RHEL:

BASH
sudo yum install git

Arch Linux:

BASH
sudo pacman -S git

openSUSE:

BASH
sudo zypper install git

(2) 3.2 Compiling from Source (To Get the Latest Version)

BASH
# 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

BASH
git --version

The output looks something like this:

TEXT
git version 2.43.0

4. Installing Git on macOS

macOS comes with Git pre-installed, but it may not be the latest version. Installing the Xcode Command Line Tools will update Git:

BASH
xcode-select --install

An installation dialog box will pop up; just click "Install."

(2) 4.2 Method 2: Using Homebrew

BASH
brew install git

(3) 4.3 Method 3: Using the Official Installation Package

  1. Visit: https://git-scm.com/download/mac
  2. Download the DMG file
  3. Double-click to install

(4) 4.4 Verify the Installation

BASH
git --version

The output looks something like this:

TEXT
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:

BASH
# 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:

BASH
git config --global core.editor "code --wait"

Configuring Vim:

BASH
git config --global core.editor "vim"

Configuring Nano:

BASH
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:

BASH
git config --global init.defaultBranch main

(4) 5.4 Configuring Line Break Conversion (Cross-Platform Collaboration)

Windows Users:

BASH
# Convert to upon detectionCRLF,Convert upon submission toLF
git config --global core.autocrlf true

Linux/macOS users:

BASH
# 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:

BASH
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:

BASH
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

BASH
git config --list

Output example:

TEXT
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

BASH
git config user.name

Output:

TEXT
Zhang San

(3) 6.3 Viewing the Location of Configuration Files

BASH
# 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

BASH
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

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

BASH
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

BASH
cat ~/.ssh/id_ed25519.pub

(3) 8.3 Adding to GitHub

  1. Copy the public key
  2. Login to GitHub → Settings → SSH and GPG keys → New SSH key
  3. Paste the public key and save it

(4) 8.4 Testing the Connection

BASH
ssh -T git@github.com

Successful output:

TEXT
Hi zhangsan! You've successfully authenticated, but GitHub does not provide shell access.

❓ FAQ

Q Why is it necessary to configure a username and email address?
A Every Git commit records the author's information (name + email) to identify who made the changes. If you forget to configure this, Git will reject the commit.
Q Which should I choose on Windows: Git Bash or PowerShell?
A Git Bash provides a full Linux command-line environment and is suitable for users accustomed to Linux; PowerShell integrates better with Windows and is suitable for Windows users. Both can be used with Git.
Q What should I do if I made a mistake in the configuration?
A Simply re-run the configuration command to overwrite it. For example: git config --global user.name "new name"
Q How do I delete a configuration item?
A Use the --unset option: git config --global --unset user.name

📖 Summary


📝 Exercises

  1. Basic Exercise: Install Git on your computer, configure your username and email address, and use git --version and git config --list to verify the installation and configuration.

  2. Advanced Exercise: Configure Git aliases; set up aliases for at least three commonly used commands, and execute these commands using the aliases.

  3. Challenge: Generate an SSH key, add it to your GitHub account, and test whether the SSH connection works.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏