MacOS 系統下 Git 的詳細安裝步驟與基礎設置指南—目錄
- 一、安裝 Git
- 方法 1:通過 Homebrew 安裝(推薦)
- 方法 2:通過 Xcode Command Line Tools 安裝
- 方法 3:手動下載安裝包
- 二、基礎配置
- 1. 設置全局用戶名和郵箱
- 2. 配置 SSH 密鑰(用于 GitHub/GitLab 等)
- 3. 配置 Git 別名(簡化命令)
- 4. 啟用 Git 自動換行符轉換
- 三、高級設置
- 1. 配置差異工具(如 Beyond Compare)
- 2. 配置 Git 代理(解決網絡問題)
- 四、常見問題與解決方法
- 1. 安裝失敗:`Error: The following directories are not writable by your user`
- 2. 權限錯誤:`Permission denied (publickey)`
- 3. Git 版本過舊
- 4. 終端提示 `git: command not found`
- 五、卸載 Git
- 六、學習資源推薦
一、安裝 Git
方法 1:通過 Homebrew 安裝(推薦)
-
安裝 Homebrew(若未安裝):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
? 安裝完成后,重啟終端。
-
通過 Homebrew 安裝 Git:
brew install git
-
驗證安裝:
git --version
? 輸出類似
git version 2.39.0
表示成功。
方法 2:通過 Xcode Command Line Tools 安裝
-
安裝 Xcode Command Line Tools:
xcode-select --install
? 系統會彈出圖形化安裝界面,按提示完成安裝。
-
驗證 Git 是否自帶安裝:
git --version
? MacOS 默認會安裝 Git,但版本可能較舊(如 2.37.0)。
方法 3:手動下載安裝包
- 訪問 Git 官網下載頁面:
https://git-scm.com/download/mac - 下載
.dmg
文件(如git-2.39.0-intel-universal-mavericks.dmg
)。 - 掛載并安裝:
? 雙擊下載的.dmg
文件,將 Git.app 拖入 Applications 文件夾。 - 配置環境變量:
# 將 Git 添加到 PATH(若手動安裝未自動配置) echo 'export PATH="/Applications/Git.app/Contents/Resources/bin:$PATH"' >> ~/.zshrc source ~/.zshrc
二、基礎配置
1. 設置全局用戶名和郵箱
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
? 驗證配置:
git config --global --list
2. 配置 SSH 密鑰(用于 GitHub/GitLab 等)
-
生成 SSH 密鑰:
ssh-keygen -t ed25519 -C "your.email@example.com"
? 按提示保存密鑰到默認路徑(
~/.ssh/id_ed25519
)。
? 設置密鑰密碼(可選)。 -
將公鑰添加到 GitHub/GitLab:
? 復制公鑰內容:cat ~/.ssh/id_ed25519.pub
? 登錄 GitHub → Settings → SSH and GPG Keys → 添加新 SSH Key。
-
測試 SSH 連接:
ssh -T git@github.com
? 成功提示:
Hi username! You've successfully authenticated.
3. 配置 Git 別名(簡化命令)
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --all"
? 示例:git st
等同于 git status
。
4. 啟用 Git 自動換行符轉換
git config --global core.autocrlf input # MacOS/Linux
git config --global core.safecrlf warn # 檢測混合換行符
三、高級設置
1. 配置差異工具(如 Beyond Compare)
- 安裝 Beyond Compare(需購買或下載試用版)。
- 配置 Git 調用 Beyond Compare:
git config --global merge.tool bc3 git config --global mergetool.bc3.path "/Applications/Beyond Compare.app/Contents/MacOS/bcomp"
2. 配置 Git 代理(解決網絡問題)
# HTTP/HTTPS 代理
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy https://127.0.0.1:7890# SOCKS5 代理(如 Clash)
git config --global http.proxy socks5://127.0.0.1:7890
git config --global https.proxy socks5://127.0.0.1:7890# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy
四、常見問題與解決方法
1. 安裝失敗:Error: The following directories are not writable by your user
? 解決:使用 sudo
或修復目錄權限:
sudo chown -R $(whoami) /usr/local/*
2. 權限錯誤:Permission denied (publickey)
? 解決:
- 確認 SSH 密鑰已添加到
ssh-agent
:eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519
- 檢查公鑰是否正確添加到 GitHub/GitLab。
3. Git 版本過舊
? 升級 Git:
brew update && brew upgrade git
4. 終端提示 git: command not found
? 解決:
? 檢查是否已安裝 Git:which git
。
? 若未安裝,通過上述方法重新安裝。
? 確保 Git 路徑在環境變量中(echo $PATH
)。
五、卸載 Git
# 通過 Homebrew 卸載
brew uninstall git# 手動刪除(若通過 dmg 安裝)
sudo rm -rf /Applications/Git.app
sudo rm -rf /usr/local/git
六、學習資源推薦
- Pro Git 電子書(免費):
https://git-scm.com/book/zh/v2 - GitHub 官方教程:
https://guides.github.com/ - Git 命令速查表:
https://education.github.com/git-cheat-sheet-education.pdf
通過以上步驟,您可以在 MacOS 上快速安裝并配置 Git,滿足日常開發需求!