Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux 系統下 Git 安裝、配置與 GitHub 協作實戰全攻略

編輯:關於 Linux

Git 是 Linux 內核開發誕生的分佈式版本控制系統,現已成為代碼協作的事實標準。雖然大多數 Linux 發行版預裝 Git,但版本往往較舊。本文將引導您完成最新版 Git 安裝、核心配置、SSH 金鑰生成及 GitHub 典型協作流程,打造高效開發環境。

一、安裝最新版 Git

1. 包管理器快速安裝(版本可能非最新)

# 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

2. 源碼編譯安裝最新穩定版(推薦生產環境)

# 安裝構建依賴
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

3. 驗證安裝

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 金鑰配置與 GitHub 免密認證

  1. 生成 ED25519 金鑰(推薦):
    ssh-keygen -t ed25519 -C "you@example.com"
    按提示設置密碼短語(可留空),生成 ~/.ssh/id_ed25519 (私鑰) 與 ~/.ssh/id_ed25519.pub (公鑰)。
  2. 啟動 SSH Agent 並添加私鑰:
    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_ed25519
    
  3. 將公鑰添加至 GitHub:
    cat ~/.ssh/id_ed25519.pub 複製輸出內容 → GitHub Settings → SSH and GPG keys → New SSH key → 貼上保存。
  4. 驗證連接:
    ssh -T git@github.com
    出現 Hi username! You've successfully authenticated... 即成功。

四、典型 GitHub 協作流程

1. 克隆倉庫

git clone git@github.com:owner/repo.git
cd repo

2. 功能分支開發

git checkout -b feature/awesome-feature
# 編寫代碼...
git add .
git commit -m "feat: add awesome feature"
git push origin feature/awesome-feature

3. 發起 Pull Request

在 GitHub 網頁端對比分支並創建 PR,經 Code Review 後合並入主分支。

4. 同步上游更新

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

Git協作流程圖

六、常見問題排查

  • 權限被拒:確認 SSH 公鑰已正確添加,私鑰權限為 600(chmod 600 ~/.ssh/id_ed25519)。
  • warning: pull.rebase is not set:執行 git config --global pull.rebase true 消除警告。
  • 大文件提交失敗:GitHub 單文件限制 100MB,需用 Git LFS 處理大型二進制資源。

完成上述配置,您將擁有一套專業級的 Git 開發環境,無縫對接 GitHub 協作生態,專注於代碼本身的創造價值。

Copyright © Windows教程網 All Rights Reserved