跳轉到內容

Git 版本控制工具簡介與安裝

Git Installation Guide

Git 是目前世界上最先進的分佈式版本控制系統,由 Linux 之父 Linus Torvalds 於 2005 年創建。無論是個人項目還是大型團隊協作,Git 都是不可或缺的工具。本文將帶你從零開始,完成 Git 的安裝和配置。

Git 是什麼?

那麼,簡單地說,Git 究竟是怎樣的一個系統呢? 請注意接下來的內容非常重要,若你理解了 Git 的思想和基本工作原理,用起來就會知其所以然,遊刃有餘。 在學習 Git 時,請儘量理清你對其它版本管理系統已有的認識,如 CVS、Subversion 或 Perforce, 這樣能幫助你使用工具時避免發生混淆。儘管 Git 用起來與其它的版本控制系統非常相似, 但它在對信息的存儲和認知方式上卻有很大差異,理解這些差異將有助於避免使用中的困惑。

Git 的核心特點

  • 分佈式架構:每個開發者都有完整的代碼庫副本
  • 速度快:大部分操作在本地執行,無需網絡連接
  • 數據完整性:使用 SHA-1 哈希算法確保數據不被篡改
  • 分支管理:輕量級分支,切換迅速
  • 免費開源:遵循 GPL v2 許可證

Git vs 其他版本控制系統

特性GitSVNCVS
架構分佈式集中式集中式
離線工作✅ 完全支持❌ 有限支持❌ 不支持
分支速度⚡ 秒級🐢 較慢🐢 很慢
存儲空間較小較大較大
學習曲線較陡平緩平緩

在 Linux 上安裝

如果你想在 Linux 上用二進制安裝程序來安裝基本的 Git 工具,可以使用發行版包含的基礎軟件包管理工具來安裝。

sh
# 更新軟件包列表
sudo apt update

# 安裝 Git
sudo apt install git-all

# 驗證安裝
git --version
sh
# Fedora 22+ 和 RHEL 8+
sudo dnf install git-all

# CentOS 7 及更早版本
sudo yum install git-all

# 驗證安裝
git --version
sh
sudo pacman -S git

# 驗證安裝
git --version
sh
sudo zypper install git-core

# 驗證安裝
git --version

從源碼編譯安裝(高級用戶)

如果你需要最新版本的 Git,或者你的發行版提供的版本過舊,可以從源碼編譯:

bash
# 1. 安裝依賴
sudo apt install libcurl4-gnutls-dev libexpat1-dev gettext \
  libz-dev libssl-dev asciidoc xmlto docbook2x

# 2. 下載源碼
wget https://github.com/git/git/archive/refs/tags/v2.43.0.tar.gz
tar -zxf v2.43.0.tar.gz
cd git-2.43.0

# 3. 編譯和安裝
make configure
./configure --prefix=/usr
make all doc info
sudo make install install-doc install-html install-info

# 4. 驗證
git --version

在 macOS 上安裝

macOS 上有多種安裝 Git 的方式:

方法一:使用 Homebrew(推薦)

bash
# 如果還沒有安裝 Homebrew,先安裝它
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# 安裝 Git
brew install git

# 驗證安裝
git --version

# 更新 Git(當有新版本時)
brew upgrade git

方法二:使用 Xcode Command Line Tools

bash
# 安裝命令行工具(包含 Git)
xcode-select --install

# 驗證安裝
git --version

方法三:下載安裝包

  1. 訪問 Git 官方網站
  2. 下載最新的 macOS 安裝包
  3. 雙擊 .dmg 文件並按照提示安裝

配置 macOS 特定的 Git

bash
# 安裝 Git 憑證助手(用於 HTTPS 認證)
git config --global credential.helper osxkeychain

# 啟用 Git 補全
brew install bash-completion
source $(brew --prefix)/etc/bash_completion

在 Windows 上安裝

在 Windows 上安裝 Git 同樣輕鬆,請參考 官方指南

方法一:使用 Git for Windows(推薦)

  1. 下載安裝程序

  2. 運行安裝嚮導

    • 雙擊下載的 .exe 文件
    • 按照嚮導提示進行安裝
  3. 重要配置選項

    ✓ 調整 PATH 環境:選擇 "Git from the command line and also from 3rd-party software"
    ✓ HTTPS 後端:選擇 "Use the OpenSSL library"
    ✓ 行尾轉換:選擇 "Checkout Windows-style, commit Unix-style"
    ✓ 終端模擬器:選擇 "Use MinTTY"
    ✓ 默認編輯器:選擇你喜歡的編輯器(如 VS Code)
  4. 完成安裝

    • 勾選 "Launch Git Bash"
    • 點擊 "Finish"

方法二:使用 Scoop

powershell
# 安裝 Scoop(如果還沒有)
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
irm get.scoop.sh | iex

# 安裝 Git
scoop install git

# 驗證
git --version

方法三:使用 Chocolatey

powershell
# 以管理員身份運行 PowerShell
choco install git -y

# 驗證
git --version

Git Bash vs CMD vs PowerShell

安裝完成後,你有三種方式使用 Git:

  1. Git Bash(推薦)

    • 提供類 Unix 的命令行體驗
    • 支持大多數 Linux 命令
    • 最好的兼容性
  2. Command Prompt (CMD)

    • Windows 原生命令行
    • 需要手動配置 PATH
  3. PowerShell

    • 強大的 Windows 腳本環境
    • 良好的 Git 支持

首次配置 Git

安裝完成後,需要進行一些基本配置:

設置用戶信息

bash
# 設置用戶名(建議使用 GitHub 用戶名)
git config --global user.name "Your Name"

# 設置郵箱(建議使用 GitHub 郵箱)
git config --global user.email "your.email@example.com"

# 驗證配置
git config --list

配置默認分支名稱

bash
# 將默認分支名設置為 main(現代標準)
git config --global init.defaultBranch main

# 或者保持為 master(傳統命名)
git config --global init.defaultBranch master

配置文本編輯器

bash
# 使用 VS Code
git config --global core.editor "code --wait"

# 使用 Vim
git config --global core.editor "vim"

# 使用 Nano
git config --global core.editor "nano"

# 使用 Sublime Text
git config --global core.editor "subl -n -w"

配置差異工具

bash
# 使用 VS Code 作為差異工具
git config --global diff.tool vscode
git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"

# 使用 meld(Linux)
git config --global diff.tool meld
git config --global difftool.prompt false

啟用顏色輸出

bash
# 自動啟用顏色(推薦)
git config --global color.ui auto

# 或者分別配置
git config --global color.status auto
git config --global color.branch auto
git config --global color.interactive auto
git config --global color.diff auto

配置別名(提高效率)

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 --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
git config --global alias.last 'log -1 HEAD'
git config --global alias.unstage 'reset HEAD --'

# 使用別名
git st      # 等同於 git status
git co main # 等同於 git checkout main
git lg      # 漂亮的日誌輸出

驗證安裝

完成安裝和配置後,驗證一切是否正常:

bash
# 檢查 Git 版本
git --version
# 輸出示例:git version 2.43.0

# 查看配置列表
git config --list

# 查看特定配置
git config user.name
git config user.email

# 測試基本功能
mkdir test-repo
cd test-repo
git init
echo "Hello Git" > README.md
git add .
git commit -m "Initial commit"
git log
cd ..
rm -rf test-repo

常見問題排查

問題 1:git 命令找不到

Linux/macOS:

bash
# 檢查 Git 是否安裝
which git

# 如果沒有,重新安裝
sudo apt install git  # Debian/Ubuntu
brew install git      # macOS

Windows:

powershell
# 檢查 PATH 環境變量
echo $env:PATH

# 重新安裝 Git for Windows,確保選擇了正確的 PATH 選項

問題 2:權限錯誤

bash
# Linux/macOS 上的權限問題
sudo chown -R $USER:$USER ~/.gitconfig

# Windows 上以管理員身份運行 Git Bash

問題 3:SSL 證書錯誤

bash
# 臨時禁用 SSL 驗證(不推薦)
git config --global http.sslVerify false

# 更好的解決方案:更新 CA 證書
sudo apt install ca-certificates  # Debian/Ubuntu
sudo update-ca-certificates

問題 4:行尾符問題

bash
# Windows 用戶:自動轉換行尾
git config --global core.autocrlf true

# macOS/Linux 用戶:保持 Unix 行尾
git config --global core.autocrlf input

# 禁用自動轉換(如果需要)
git config --global core.autocrlf false

下一步學習

恭喜你完成了 Git 的安裝和配置!接下來可以學習:

總結

本文詳細介紹了 Git 在各大平臺上的安裝方法:

  1. Linux:使用包管理器或源碼編譯
  2. macOS:推薦 Homebrew 或 Xcode 工具
  3. Windows:推薦 Git for Windows

安裝完成後,別忘了:

  • ✅ 設置用戶名和郵箱
  • ✅ 配置默認分支名稱
  • ✅ 設置喜歡的文本編輯器
  • ✅ 啟用顏色輸出
  • ✅ 配置常用別名

現在你已經準備好開始使用 Git 進行版本控制了!🎉

最後更新於: