Git 版本控制工具簡介與安裝
Git 是目前世界上最先進的分佈式版本控制系統,由 Linux 之父 Linus Torvalds 於 2005 年創建。無論是個人項目還是大型團隊協作,Git 都是不可或缺的工具。本文將帶你從零開始,完成 Git 的安裝和配置。
Git 是什麼?
那麼,簡單地說,Git 究竟是怎樣的一個系統呢? 請注意接下來的內容非常重要,若你理解了 Git 的思想和基本工作原理,用起來就會知其所以然,遊刃有餘。 在學習 Git 時,請儘量理清你對其它版本管理系統已有的認識,如 CVS、Subversion 或 Perforce, 這樣能幫助你使用工具時避免發生混淆。儘管 Git 用起來與其它的版本控制系統非常相似, 但它在對信息的存儲和認知方式上卻有很大差異,理解這些差異將有助於避免使用中的困惑。
Git 的核心特點
- 分佈式架構:每個開發者都有完整的代碼庫副本
- 速度快:大部分操作在本地執行,無需網絡連接
- 數據完整性:使用 SHA-1 哈希算法確保數據不被篡改
- 分支管理:輕量級分支,切換迅速
- 免費開源:遵循 GPL v2 許可證
Git vs 其他版本控制系統
| 特性 | Git | SVN | CVS |
|---|---|---|---|
| 架構 | 分佈式 | 集中式 | 集中式 |
| 離線工作 | ✅ 完全支持 | ❌ 有限支持 | ❌ 不支持 |
| 分支速度 | ⚡ 秒級 | 🐢 較慢 | 🐢 很慢 |
| 存儲空間 | 較小 | 較大 | 較大 |
| 學習曲線 | 較陡 | 平緩 | 平緩 |
在 Linux 上安裝
如果你想在 Linux 上用二進制安裝程序來安裝基本的 Git 工具,可以使用發行版包含的基礎軟件包管理工具來安裝。
# 更新軟件包列表
sudo apt update
# 安裝 Git
sudo apt install git-all
# 驗證安裝
git --version# Fedora 22+ 和 RHEL 8+
sudo dnf install git-all
# CentOS 7 及更早版本
sudo yum install git-all
# 驗證安裝
git --versionsudo pacman -S git
# 驗證安裝
git --versionsudo zypper install git-core
# 驗證安裝
git --version從源碼編譯安裝(高級用戶)
如果你需要最新版本的 Git,或者你的發行版提供的版本過舊,可以從源碼編譯:
# 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(推薦)
# 如果還沒有安裝 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
# 安裝命令行工具(包含 Git)
xcode-select --install
# 驗證安裝
git --version方法三:下載安裝包
- 訪問 Git 官方網站
- 下載最新的 macOS 安裝包
- 雙擊
.dmg文件並按照提示安裝
配置 macOS 特定的 Git
# 安裝 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(推薦)
下載安裝程序
- 訪問 https://gitforwindows.org/
- 下載最新的安裝程序
運行安裝嚮導
- 雙擊下載的
.exe文件 - 按照嚮導提示進行安裝
- 雙擊下載的
重要配置選項
✓ 調整 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)完成安裝
- 勾選 "Launch Git Bash"
- 點擊 "Finish"
方法二:使用 Scoop
# 安裝 Scoop(如果還沒有)
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
irm get.scoop.sh | iex
# 安裝 Git
scoop install git
# 驗證
git --version方法三:使用 Chocolatey
# 以管理員身份運行 PowerShell
choco install git -y
# 驗證
git --versionGit Bash vs CMD vs PowerShell
安裝完成後,你有三種方式使用 Git:
Git Bash(推薦)
- 提供類 Unix 的命令行體驗
- 支持大多數 Linux 命令
- 最好的兼容性
Command Prompt (CMD)
- Windows 原生命令行
- 需要手動配置 PATH
PowerShell
- 強大的 Windows 腳本環境
- 良好的 Git 支持
首次配置 Git
安裝完成後,需要進行一些基本配置:
設置用戶信息
# 設置用戶名(建議使用 GitHub 用戶名)
git config --global user.name "Your Name"
# 設置郵箱(建議使用 GitHub 郵箱)
git config --global user.email "your.email@example.com"
# 驗證配置
git config --list配置默認分支名稱
# 將默認分支名設置為 main(現代標準)
git config --global init.defaultBranch main
# 或者保持為 master(傳統命名)
git config --global init.defaultBranch master配置文本編輯器
# 使用 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"配置差異工具
# 使用 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啟用顏色輸出
# 自動啟用顏色(推薦)
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配置別名(提高效率)
# 常用別名
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 # 漂亮的日誌輸出驗證安裝
完成安裝和配置後,驗證一切是否正常:
# 檢查 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:
# 檢查 Git 是否安裝
which git
# 如果沒有,重新安裝
sudo apt install git # Debian/Ubuntu
brew install git # macOSWindows:
# 檢查 PATH 環境變量
echo $env:PATH
# 重新安裝 Git for Windows,確保選擇了正確的 PATH 選項問題 2:權限錯誤
# Linux/macOS 上的權限問題
sudo chown -R $USER:$USER ~/.gitconfig
# Windows 上以管理員身份運行 Git Bash問題 3:SSL 證書錯誤
# 臨時禁用 SSL 驗證(不推薦)
git config --global http.sslVerify false
# 更好的解決方案:更新 CA 證書
sudo apt install ca-certificates # Debian/Ubuntu
sudo update-ca-certificates問題 4:行尾符問題
# Windows 用戶:自動轉換行尾
git config --global core.autocrlf true
# macOS/Linux 用戶:保持 Unix 行尾
git config --global core.autocrlf input
# 禁用自動轉換(如果需要)
git config --global core.autocrlf false下一步學習
恭喜你完成了 Git 的安裝和配置!接下來可以學習:
總結
本文詳細介紹了 Git 在各大平臺上的安裝方法:
- Linux:使用包管理器或源碼編譯
- macOS:推薦 Homebrew 或 Xcode 工具
- Windows:推薦 Git for Windows
安裝完成後,別忘了:
- ✅ 設置用戶名和郵箱
- ✅ 配置默認分支名稱
- ✅ 設置喜歡的文本編輯器
- ✅ 啟用顏色輸出
- ✅ 配置常用別名
現在你已經準備好開始使用 Git 進行版本控制了!🎉