首页
找靠谱产品
找解决方案
找靠谱公司
找案例
找对的人
专家智库
悬赏任务
SAAS
ToB门户
了解全球最新的ToB事件
论坛
潜水/灌水快乐,沉淀知识,认识更多同行。
ToB圈子
加入IT圈,遇到更多同好之人。
微博
Follow
记录
Doing
博客
Blog
文库
业界最专业的IT文库,上传资料也可以赚钱
下载
分享
Share
排行榜
Ranklist
相册
Album
应用中心
qidao123.com ToB IT社区-企服评测·应用市场
»
论坛
›
运维.售后
›
运维.售后
›
MacOS 系统下 Git 的详细安装步骤与基础设置指南 ...
返回列表
发新帖
MacOS 系统下 Git 的详细安装步骤与基础设置指南
[复制链接]
发表于 2025-5-17 22:15:03
|
显示全部楼层
|
阅读模式
一、安装 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,满足日常开发需求!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
本帖子中包含更多资源
您需要
登录
才可以下载或查看,没有账号?
立即注册
×
回复
使用道具
举报
返回列表
盛世宏图
+ 我要发帖
登录后关闭弹窗
登录参与点评抽奖 加入IT实名职场社区
去登录
微信订阅号
微信服务号
微信客服(加群)
H5
小程序
快速回复
返回顶部
返回列表