前置条件
- 一台运行 macOS 的电脑
- 已安装 Xcode Command Line Tools(Git 和编译工具依赖)
- GitHub、GitLab 或其他 Git 平台账号
- 项目仓库地址(可为 SSH 或 HTTPS 格式)
1. 安装 Git
方法一:通过 Homebrew 安装(推荐)
brew install git
安装完成后,输入以下命令检查是否安装成功:
git --version
输出示例:
git version 2.42.0
方法二:通过 Xcode 命令行工具安装
xcode-select --install
安装完成后即可使用 Git。
2. 配置 Git
首次使用 Git,需配置用户名和邮箱:
git config --global user.name "你的用户名"
git config --global user.email "你的邮箱@example.com"
查看当前的 Git 配置信息:
git config --global --list
3. 生成 SSH Key(推荐方式)
为了避免频繁输入用户名和密码,建议使用 SSH 方式连接仓库。
生成 SSH 密钥
ssh-keygen -t ed25519 -C "你的邮箱@example.com"
按提示一路回车,默认会生成以下两个文件:
- 私钥:
~/.ssh/id_ed25519 - 公钥:
~/.ssh/id_ed25519.pub
启动 SSH agent 并添加私钥
eval "$(ssh-agent -s)" ssh-add --apple-use-keychain ~/.ssh/id_ed25519
4. 将 SSH Key 添加到 GitHub/GitLab
复制公钥内容到剪贴板
pbcopy < ~/.ssh/id_ed25519.pub
添加到 Git 平台
- GitHub: 登录后进入 https://github.com/settings/keys
- GitLab: 登录后进入 https://gitlab.com/-/profile/keys


