Git 是 Linux 內核開發誕生的分佈式版本控制系統,現已成為代碼協作的事實標準。雖然大多數 Linux 發行版預裝 Git,但版本往往較舊。本文將引導您完成最新版 Git 安裝、核心配置、SSH 金鑰生成及 GitHub 典型協作流程,打造高效開發環境。
# Debian/Ubuntu
sudo apt-get update && sudo apt-get install git
# RHEL/CentOS/Fedora
sudo yum install git # 或 dnf install git
# Arch Linux
sudo pacman -S git
# 安裝構建依賴
sudo yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel asciidoc xmlto docbook2x # RHEL系
sudo apt-get install libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev asciidoc xmlto docbook2x # Debian系
# 下載並編譯
cd /usr/local/src
wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.45.0.tar.xz # 替換為最新版
tar xf git-2.45.0.tar.xz
cd git-2.45.0
make prefix=/usr/local/git all
sudo make prefix=/usr/local/git install
# 配置環境變量
echo 'export PATH=/usr/local/git/bin:$PATH' | sudo tee /etc/profile.d/git.sh
source /etc/profile.d/git.sh
git --version
# 輸出示例:git version 2.45.0
提交記錄會永久嵌入作者信息,請務必正確設置:
# 全局配置(適用於當前用戶所有倉庫)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# 查看當前配置
git config --list
# 單倉庫覆蓋(進入倉庫目錄後執行,不加 --global)
git config user.name "Project Specific Name"
git config user.email "project@example.com"
ssh-keygen -t ed25519 -C "you@example.com"~/.ssh/id_ed25519 (私鑰) 與 ~/.ssh/id_ed25519.pub (公鑰)。eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
cat ~/.ssh/id_ed25519.pub 複製輸出內容 → GitHub Settings → SSH and GPG keys → New SSH key → 貼上保存。ssh -T git@github.comHi username! You've successfully authenticated... 即成功。git clone git@github.com:owner/repo.git
cd repo
git checkout -b feature/awesome-feature
# 編寫代碼...
git add .
git commit -m "feat: add awesome feature"
git push origin feature/awesome-feature
在 GitHub 網頁端對比分支並創建 PR,經 Code Review 後合並入主分支。
git remote add upstream git@github.com:upstream/repo.git
git fetch upstream
git checkout main
git merge upstream/main
git push origin main
編輯 ~/.gitconfig 添加:
[alias]
st = status
co = checkout
br = branch
cm = commit -m
lg = log --oneline --graph --all --decorate
last = log -1 HEAD
unstage = reset HEAD --
[core]
editor = vim
autocrlf = input
[pull]
rebase = true

chmod 600 ~/.ssh/id_ed25519)。warning: pull.rebase is not set:執行 git config --global pull.rebase true 消除警告。完成上述配置,您將擁有一套專業級的 Git 開發環境,無縫對接 GitHub 協作生態,專注於代碼本身的創造價值。