MacOS系统Git安装与配置指南

Git 是 MacOS 系统下开发者必备的版本控制工具,高效管理代码提交、分支管理等核心操作。本文将详细介绍 MacOS 系统中 Git 的三种安装方法,包括 Homebrew 推荐安装、Xcode Command Line Tools 快捷安装及手动安装,搭配完整的基础配置、高级设置及常见问题解决方案,助力用户快速完成 Git 部署与使用。如果你是 Windows 系统在安装 git 可以看《Windows 系统 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 官网下载页面;
- 下载
.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 等)
(1)生成 SSH 密钥
ssh-keygen -t ed25519 -C "your.email@example.com"
- 按提示保存密钥到默认路径(
~/.ssh/id_ed25519)。 - 设置密钥密码(可选)。
(2)将公钥添加到 GitHub/GitLab
复制公钥内容:
cat ~/.ssh/id_ed25519.pub
- 登录 GitHub → Settings → SSH and GPG Keys → 添加新 SSH Key。
(3)测试 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
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
结语
以上就是 MacOS 系统下 Git 的完整安装步骤、基础配置、高级设置及常见问题解决方法,涵盖 Homebrew 安装、SSH 密钥配置、代理设置等关键操作。按照指南操作,可轻松完成 Git 的安装与调试,高效开展代码版本控制工作,后续可通过git help进一步探索更多实用功能,提升开发效率。
以上关于MacOS系统Git安装与配置指南的文章就介绍到这了,更多相关内容请搜索码云笔记以前的文章或继续浏览下面的相关文章,希望大家以后多多支持码云笔记。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 admin@mybj123.com 进行投诉反馈,一经查实,立即处理!
重要:如软件存在付费、会员、充值等,均属软件开发者或所属公司行为,与本站无关,网友需自行判断
码云笔记 » MacOS系统Git安装与配置指南
微信
支付宝