欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 国际 > MacOS 系统下 Git 的详细安装步骤与基础设置指南

MacOS 系统下 Git 的详细安装步骤与基础设置指南

2025/4/19 12:22:17 来源:https://blog.csdn.net/qq_45657541/article/details/147254256  浏览:    关键词:MacOS 系统下 Git 的详细安装步骤与基础设置指南

在这里插入图片描述


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 安装(推荐)

  1. 安装 Homebrew(若未安装):

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    

    • 安装完成后,重启终端。

  2. 通过 Homebrew 安装 Git:

    brew install git
    
  3. 验证安装:

    git --version
    

    • 输出类似 git version 2.39.0 表示成功。


方法 2:通过 Xcode Command Line Tools 安装

  1. 安装 Xcode Command Line Tools:

    xcode-select --install
    

    • 系统会弹出图形化安装界面,按提示完成安装。

  2. 验证 Git 是否自带安装:

    git --version
    

    • MacOS 默认会安装 Git,但版本可能较旧(如 2.37.0)。


方法 3:手动下载安装包

  1. 访问 Git 官网下载页面:
    https://git-scm.com/download/mac
  2. 下载 .dmg 文件(如 git-2.39.0-intel-universal-mavericks.dmg)。
  3. 挂载并安装:
    • 双击下载的 .dmg 文件,将 Git.app 拖入 Applications 文件夹。
  4. 配置环境变量:
    # 将 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)

  1. 安装 Beyond Compare(需购买或下载试用版)。
  2. 配置 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)

• 解决:

  1. 确认 SSH 密钥已添加到 ssh-agent
    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_ed25519
    
  2. 检查公钥是否正确添加到 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

六、学习资源推荐

  1. Pro Git 电子书(免费):
    https://git-scm.com/book/zh/v2
  2. GitHub 官方教程:
    https://guides.github.com/
  3. Git 命令速查表:
    https://education.github.com/git-cheat-sheet-education.pdf

通过以上步骤,您可以在 MacOS 上快速安装并配置 Git,满足日常开发需求!


版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词