Git 2 安装教程:Windows、macOS 与 Linux 完整指南

2026-12-01 11:43:11
Git 是目前最常用的分布式版本控制系统,广泛应用于软件开发、代码托管和团队协作。本教程将介绍 Git 2.x 在 Windows、macOS 和 Linux 系统上的安装方法,并完成基础配置与安装验证。 --- ## 一、安装前准备 在安装 Git 之前,可以先确认电脑是否已经安装过 Git。 打开终端或命令提示符,执行: ```bash git --version ``` 如果终端返回类似以下内容,说明 Git 已经安装: ```bash git version 2.x.x ``` 如果提示“找不到命令”或“不是内部或外部命令”,则需要继续安装。 --- ## 二、Windows 安装 Git 2 ### 方法一:使用安装程序 1. 下载 Git for Windows 安装程序。 2. 双击运行安装文件。 3. 根据向导提示完成安装。 安装过程中大部分选项可以保持默认,重点选项如下: ### 1. 选择默认编辑器 Git 会询问默认文本编辑器,可以选择: - Vim - Visual Studio Code - Notepad - Notepad++ 如果平时使用 Visual Studio Code,可以选择: ```text Use Visual Studio Code as Git's default editor ``` ### 2. 配置 PATH 环境变量 建议选择: ```text Git from the command line and also from 3rd-party software ``` 这样可以在命令提示符、PowerShell 和其他开发工具中使用 Git。 ### 3. 选择 HTTPS 连接方式 一般保持默认: ```text Use the OpenSSL library ``` ### 4. 配置换行符转换 Windows 用户通常可以选择: ```text Checkout Windows-style, commit Unix-style line endings ``` 该选项可以减少 Windows 和 Linux 系统之间的换行符冲突。 ### 5. 完成安装 继续点击 **Next**,最后点击 **Install**,等待安装完成即可。 --- ### 方法二:使用 Windows 包管理器 如果系统支持 `winget`,可以打开 PowerShell,执行: ```powershell winget install --id Git.Git -e --source winget ``` 安装完成后,重新打开命令提示符或 PowerShell。 --- ### Windows 验证安装 打开 Git Bash、PowerShell 或命令提示符,执行: ```bash git --version ``` 如果显示 Git 版本号,说明安装成功。 --- ## 三、macOS 安装 Git 2 ### 方法一:使用 Homebrew 安装 如果电脑已经安装 Homebrew,可以执行: ```bash brew install git ``` 安装完成后查看版本: ```bash git --version ``` 如果想让 Homebrew 安装的 Git 优先于系统自带版本,可以执行: ```bash echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.zshrc source ~/.zshrc ``` Intel 芯片 Mac 通常使用以下路径: ```bash echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.zshrc source ~/.zshrc ``` --- ### 方法二:安装 Xcode Command Line Tools macOS 也可以通过安装开发者命令行工具获得 Git: ```bash xcode-select --install ``` 在弹出的窗口中确认安装即可。 安装完成后执行: ```bash git --version ``` --- ### macOS 常见问题 如果执行 `git` 时出现安装提示,可以按照系统提示安装 Command Line Tools。 如果安装了多个 Git,可以使用以下命令查看当前使用的 Git 路径: ```bash which git ``` 查看 Git 的实际版本: ```bash git --version ``` --- ## 四、Linux 安装 Git 2 ### Ubuntu 或 Debian 执行: ```bash sudo apt update sudo apt install git ``` 安装完成后验证: ```bash git --version ``` 如果希望安装官方源中较新的版本,也可以使用 Git PPA: ```bash sudo add-apt-repository ppa:git-core/ppa sudo apt update sudo apt install git ``` --- ### Fedora 执行: ```bash sudo dnf install git ``` 验证安装: ```bash git --version ``` --- ### CentOS 或 RHEL 较新的系统可以使用: ```bash sudo dnf install git ``` 旧版本系统可以使用: ```bash sudo yum install git ``` --- ### Arch Linux 执行: ```bash sudo pacman -S git ``` 验证安装: ```bash git --version ``` --- ## 五、安装后的基础配置 Git 安装完成后,建议立即设置用户名和邮箱。 ### 1. 设置用户名 ```bash git config --global user.name "你的用户名" ``` 例如: ```bash git config --global user.name "Zhang San" ``` ### 2. 设置邮箱 ```bash git config --global user.email "your_email@example.com" ``` 建议使用与代码托管平台绑定的邮箱。 ### 3. 查看配置 ```bash git config --global --list ``` 也可以分别查看: ```bash git config user.name git config user.email ``` --- ## 六、设置默认分支名称 现在很多代码托管平台默认使用 `main` 作为主分支名称,可以执行: ```bash git config --global init.defaultBranch main ``` 之后新建 Git 仓库时,默认分支将自动命名为 `main`。 --- ## 七、设置换行符处理方式 ### Windows 用户 ```bash git config --global core.autocrlf true ``` ### macOS 或 Linux 用户 ```bash git config --global core.autocrlf input ``` 该配置可以减少不同操作系统之间因换行符不同导致的文件变化。 --- ## 八、验证 Git 是否正常工作 可以创建一个测试目录: ```bash mkdir git-test cd git-test ``` 初始化 Git 仓库: ```bash git init ``` 创建测试文件: ```bash echo "# Git Test" > README.md ``` 查看仓库状态: ```bash git status ``` 将文件添加到暂存区: ```bash git add README.md ``` 提交文件: ```bash git commit -m "Initial commit" ``` 如果提交成功,说明 Git 已经可以正常使用。 --- ## 九、常用 Git 命令 | 命令 | 作用 | |---|---| | `git init` | 初始化本地仓库 | | `git clone 仓库地址` | 克隆远程仓库 | | `git status` | 查看文件状态 | | `git add 文件名` | 将文件加入暂存区 | | `git add .` | 添加当前目录下的所有变化 | | `git commit -m "说明"` | 创建提交 | | `git log` | 查看提交记录 | | `git branch` | 查看分支 | | `git switch 分支名` | 切换分支 | | `git pull` | 拉取远程更新 | | `git push` | 推送本地提交 | | `git remote -v` | 查看远程仓库地址 | --- ## 十、常见问题及解决方法 ### 1. 提示“git 不是内部或外部命令” 可能原因: - Git 尚未安装; - Git 没有加入环境变量; - 终端窗口没有重新打开。 解决方法: 1. 重新安装 Git; 2. 确认安装时选择了命令行支持; 3. 关闭并重新打开终端; 4. Windows 用户也可以尝试使用 Git Bash。 --- ### 2. 提示无法识别用户身份 如果出现类似错误: ```text Please tell me who you are ``` 说明还没有配置用户名和邮箱。 执行: ```bash git config --global user.name "你的用户名" git config --global user.email "你的邮箱" ``` --- ### 3. `git --version` 显示的版本不是最新版本 可以先查看 Git 的安装位置: ```bash which git ``` Windows 下可以执行: ```powershell where.exe git ``` 如果系统中安装了多个 Git,需要调整环境变量顺序,确保使用正确的版本。 --- ### 4. 推送代码时要求输入密码 许多代码托管平台已经不再支持直接使用账户密码进行 Git 操作。通常需要使用: - Personal Access Token,即个人访问令牌; - SSH 密钥; - 官方命令行工具。 如果经常使用 Git,建议配置 SSH 密钥,以减少重复输入认证信息。 --- ## 十一、总结 Git 2 的安装流程主要包括以下步骤: 1. 根据操作系统选择安装方式; 2. 执行 `git --version` 验证安装; 3. 配置用户名和邮箱; 4. 设置默认分支及换行符规则; 5. 创建测试仓库并完成第一次提交。 完成以上配置后,就可以使用 Git 进行代码版本管理、分支协作以及远程仓库操作了。

相关文章