Git安装与配置全攻略:从入门到高效使用指南

一、Git安装前的准备与系统适配

1.1 安装前环境检查

在安装Git前,需确认操作系统版本是否符合要求。Windows用户需确保系统为Windows 7及以上版本,macOS需为10.12(Sierra)或更高,Linux则需支持终端操作(如Ubuntu 18.04+)。建议关闭可能冲突的版本控制工具(如旧版SVN客户端),避免端口或配置文件冲突。

1.2 安装包选择与下载

  • Windows:推荐从Git官网下载64位版本,包含Git Bash(类Linux终端)和GUI工具(Git GUI)。
  • macOS:可通过Homebrew安装(brew install git)或从官网下载.pkg安装包。
  • Linux:Debian/Ubuntu用户使用sudo apt install git,CentOS/RHEL用户使用sudo yum install git

关键提示:官网下载时避免选择第三方捆绑包,确保安装包来源纯净。

二、Git安装流程详解

2.1 Windows系统安装

  1. 运行安装程序:双击下载的.exe文件,选择安装路径(建议默认)。
  2. 组件选择
    • 勾选Git Bash HereGit GUI Here,方便右键快速访问。
    • 选择Use the OpenSSL library(默认)以支持HTTPS协议。
  3. 调整PATH环境变量
    • 推荐选择Use Git from Git Bash only(避免与系统CMD冲突)。
    • 高级用户可选Use Git and optional Unix tools from the Command Prompt(需谨慎配置)。
  4. 配置行尾符:选择Checkout as-is, commit Unix-style line endings(跨平台协作最佳实践)。
  5. 终端模拟器:建议使用MinTTY(支持256色和Unicode)。

2.2 macOS系统安装

  • Homebrew安装(推荐):
    1. brew install git

    优势:自动处理依赖,支持版本回滚。

  • 官网.pkg安装:双击安装包,按向导完成,适合无Homebrew环境。

2.3 Linux系统安装

  • Debian/Ubuntu
    1. sudo apt update
    2. sudo apt install git -y
  • CentOS/RHEL
    1. sudo yum install git -y

    验证安装:运行git --version,应显示类似git version 2.25.1

三、Git核心配置步骤

3.1 全局用户信息配置

首次使用Git前,必须配置用户名和邮箱(提交记录的标识):

  1. git config --global user.name "Your Name"
  2. git config --global user.email "your.email@example.com"
  • 作用域说明
    • --global:对当前用户所有仓库生效(存储在~/.gitconfig)。
    • --local:仅对当前仓库生效(存储在.git/config)。

3.2 配置默认编辑器

设置提交信息编辑器(如VS Code):

  1. git config --global core.editor "code --wait"

或使用Vim/Nano:

  1. git config --global core.editor "vim"

3.3 忽略文件配置

创建全局忽略文件(如忽略.DS_Store*.log):

  1. git config --global core.excludesfile ~/.gitignore_global
  2. echo ".DS_Store" >> ~/.gitignore_global
  3. echo "*.log" >> ~/.gitignore_global

四、SSH密钥生成与配置

4.1 生成SSH密钥对

  1. ssh-keygen -t ed25519 -C "your.email@example.com"
  • 参数说明
    • -t ed25519:推荐算法(安全性高于RSA)。
    • -C:添加注释(通常为邮箱)。
  • 交互提示
    • 输入密钥保存路径(默认~/.ssh/id_ed25519)。
    • 设置密码(可选,增强安全性)。

4.2 添加密钥到SSH代理

  1. eval "$(ssh-agent -s)"
  2. ssh-add ~/.ssh/id_ed25519

4.3 配置GitHub/GitLab等平台

  1. 复制公钥内容:
    1. cat ~/.ssh/id_ed25519.pub
  2. 登录GitHub,进入Settings → SSH and GPG keys → New SSH Key,粘贴公钥。
  3. 测试连接:
    1. ssh -T git@github.com

    成功应显示:Hi username! You've successfully authenticated...

五、IDE集成与高级配置

5.1 VS Code集成

  1. 安装GitLens扩展(增强代码追溯)。
  2. 配置默认终端为Git Bash(Windows):
    • 打开VS Code设置(Ctrl+,),搜索terminal.integrated.shell.windows,添加:
      1. "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"

5.2 IntelliJ IDEA集成

  1. 安装Git插件(通常内置)。
  2. 配置Git可执行文件路径:
    • File → Settings → Version Control → Git,指定git.exe路径(如C:\Program Files\Git\bin\git.exe)。

5.3 代理配置(适用于内网环境)

  1. git config --global http.proxy "http://proxy.example.com:8080"
  2. git config --global https.proxy "http://proxy.example.com:8080"

取消代理:

  1. git config --global --unset http.proxy

六、常见问题与解决方案

6.1 安装失败处理

  • Windows错误代码2503/2502:以管理员身份运行安装程序。
  • macOS提示“损坏”:在系统偏好设置 → 安全性与隐私中允许安装。

6.2 SSH连接超时

  • 检查~/.ssh/config是否包含错误配置。
  • 测试端口连通性:
    1. telnet github.com 22

6.3 提交记录未关联用户

若忘记配置user.name/user.email,提交记录会显示匿名。修正后需重写历史:

  1. git filter-branch --env-filter '
  2. export GIT_AUTHOR_NAME="Your Name"
  3. export GIT_AUTHOR_EMAIL="your.email@example.com"
  4. ' HEAD

七、总结与最佳实践

  1. 统一配置:使用--global配置避免重复设置。
  2. 密钥管理:定期轮换SSH密钥,备份私钥。
  3. 性能优化:对大型仓库启用git config --global core.preloadindex true
  4. 安全建议:禁用git config --global credential.helper store(明文存储密码),推荐使用manager-core(Windows)或osxkeychain(macOS)。

通过以上步骤,开发者可快速完成Git的安装与基础配置,为后续的版本控制、协作开发奠定坚实基础。