跳转到内容

Git 版本控制工具简介与安装

Git Installation Guide

Git 是目前世界上最先进的分布式版本控制系统,由 Linux 之父 Linus Torvalds 于 2005 年创建。无论是个人项目还是大型团队协作,Git 都是不可或缺的工具。本文将带你从零开始,完成 Git 的安装和配置。

Git 是什么?

那么,简单地说,Git 究竟是怎样的一个系统呢? 请注意接下来的内容非常重要,若你理解了 Git 的思想和基本工作原理,用起来就会知其所以然,游刃有余。 在学习 Git 时,请尽量理清你对其它版本管理系统已有的认识,如 CVS、Subversion 或 Perforce, 这样能帮助你使用工具时避免发生混淆。尽管 Git 用起来与其它的版本控制系统非常相似, 但它在对信息的存储和认知方式上却有很大差异,理解这些差异将有助于避免使用中的困惑。

Git 的核心特点

  • 分布式架构:每个开发者都有完整的代码库副本
  • 速度快:大部分操作在本地执行,无需网络连接
  • 数据完整性:使用 SHA-1 哈希算法确保数据不被篡改
  • 分支管理:轻量级分支,切换迅速
  • 免费开源:遵循 GPL v2 许可证

Git vs 其他版本控制系统

特性GitSVNCVS
架构分布式集中式集中式
离线工作✅ 完全支持❌ 有限支持❌ 不支持
分支速度⚡ 秒级🐢 较慢🐢 很慢
存储空间较小较大较大
学习曲线较陡平缓平缓

在 Linux 上安装

如果你想在 Linux 上用二进制安装程序来安装基本的 Git 工具,可以使用发行版包含的基础软件包管理工具来安装。

sh
# 更新软件包列表
sudo apt update

# 安装 Git
sudo apt install git-all

# 验证安装
git --version
sh
# Fedora 22+ 和 RHEL 8+
sudo dnf install git-all

# CentOS 7 及更早版本
sudo yum install git-all

# 验证安装
git --version
sh
sudo pacman -S git

# 验证安装
git --version
sh
sudo zypper install git-core

# 验证安装
git --version

从源码编译安装(高级用户)

如果你需要最新版本的 Git,或者你的发行版提供的版本过旧,可以从源码编译:

bash
# 1. 安装依赖
sudo apt install libcurl4-gnutls-dev libexpat1-dev gettext \
  libz-dev libssl-dev asciidoc xmlto docbook2x

# 2. 下载源码
wget https://github.com/git/git/archive/refs/tags/v2.43.0.tar.gz
tar -zxf v2.43.0.tar.gz
cd git-2.43.0

# 3. 编译和安装
make configure
./configure --prefix=/usr
make all doc info
sudo make install install-doc install-html install-info

# 4. 验证
git --version

在 macOS 上安装

macOS 上有多种安装 Git 的方式:

方法一:使用 Homebrew(推荐)

bash
# 如果还没有安装 Homebrew,先安装它
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# 安装 Git
brew install git

# 验证安装
git --version

# 更新 Git(当有新版本时)
brew upgrade git

方法二:使用 Xcode Command Line Tools

bash
# 安装命令行工具(包含 Git)
xcode-select --install

# 验证安装
git --version

方法三:下载安装包

  1. 访问 Git 官方网站
  2. 下载最新的 macOS 安装包
  3. 双击 .dmg 文件并按照提示安装

配置 macOS 特定的 Git

bash
# 安装 Git 凭证助手(用于 HTTPS 认证)
git config --global credential.helper osxkeychain

# 启用 Git 补全
brew install bash-completion
source $(brew --prefix)/etc/bash_completion

在 Windows 上安装

在 Windows 上安装 Git 同样轻松,请参考 官方指南

方法一:使用 Git for Windows(推荐)

  1. 下载安装程序

  2. 运行安装向导

    • 双击下载的 .exe 文件
    • 按照向导提示进行安装
  3. 重要配置选项

    ✓ 调整 PATH 环境:选择 "Git from the command line and also from 3rd-party software"
    ✓ HTTPS 后端:选择 "Use the OpenSSL library"
    ✓ 行尾转换:选择 "Checkout Windows-style, commit Unix-style"
    ✓ 终端模拟器:选择 "Use MinTTY"
    ✓ 默认编辑器:选择你喜欢的编辑器(如 VS Code)
  4. 完成安装

    • 勾选 "Launch Git Bash"
    • 点击 "Finish"

方法二:使用 Scoop

powershell
# 安装 Scoop(如果还没有)
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
irm get.scoop.sh | iex

# 安装 Git
scoop install git

# 验证
git --version

方法三:使用 Chocolatey

powershell
# 以管理员身份运行 PowerShell
choco install git -y

# 验证
git --version

Git Bash vs CMD vs PowerShell

安装完成后,你有三种方式使用 Git:

  1. Git Bash(推荐)

    • 提供类 Unix 的命令行体验
    • 支持大多数 Linux 命令
    • 最好的兼容性
  2. Command Prompt (CMD)

    • Windows 原生命令行
    • 需要手动配置 PATH
  3. PowerShell

    • 强大的 Windows 脚本环境
    • 良好的 Git 支持

首次配置 Git

安装完成后,需要进行一些基本配置:

设置用户信息

bash
# 设置用户名(建议使用 GitHub 用户名)
git config --global user.name "Your Name"

# 设置邮箱(建议使用 GitHub 邮箱)
git config --global user.email "your.email@example.com"

# 验证配置
git config --list

配置默认分支名称

bash
# 将默认分支名设置为 main(现代标准)
git config --global init.defaultBranch main

# 或者保持为 master(传统命名)
git config --global init.defaultBranch master

配置文本编辑器

bash
# 使用 VS Code
git config --global core.editor "code --wait"

# 使用 Vim
git config --global core.editor "vim"

# 使用 Nano
git config --global core.editor "nano"

# 使用 Sublime Text
git config --global core.editor "subl -n -w"

配置差异工具

bash
# 使用 VS Code 作为差异工具
git config --global diff.tool vscode
git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"

# 使用 meld(Linux)
git config --global diff.tool meld
git config --global difftool.prompt false

启用颜色输出

bash
# 自动启用颜色(推荐)
git config --global color.ui auto

# 或者分别配置
git config --global color.status auto
git config --global color.branch auto
git config --global color.interactive auto
git config --global color.diff auto

配置别名(提高效率)

bash
# 常用别名
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
git config --global alias.last 'log -1 HEAD'
git config --global alias.unstage 'reset HEAD --'

# 使用别名
git st      # 等同于 git status
git co main # 等同于 git checkout main
git lg      # 漂亮的日志输出

验证安装

完成安装和配置后,验证一切是否正常:

bash
# 检查 Git 版本
git --version
# 输出示例:git version 2.43.0

# 查看配置列表
git config --list

# 查看特定配置
git config user.name
git config user.email

# 测试基本功能
mkdir test-repo
cd test-repo
git init
echo "Hello Git" > README.md
git add .
git commit -m "Initial commit"
git log
cd ..
rm -rf test-repo

常见问题排查

问题 1:git 命令找不到

Linux/macOS:

bash
# 检查 Git 是否安装
which git

# 如果没有,重新安装
sudo apt install git  # Debian/Ubuntu
brew install git      # macOS

Windows:

powershell
# 检查 PATH 环境变量
echo $env:PATH

# 重新安装 Git for Windows,确保选择了正确的 PATH 选项

问题 2:权限错误

bash
# Linux/macOS 上的权限问题
sudo chown -R $USER:$USER ~/.gitconfig

# Windows 上以管理员身份运行 Git Bash

问题 3:SSL 证书错误

bash
# 临时禁用 SSL 验证(不推荐)
git config --global http.sslVerify false

# 更好的解决方案:更新 CA 证书
sudo apt install ca-certificates  # Debian/Ubuntu
sudo update-ca-certificates

问题 4:行尾符问题

bash
# Windows 用户:自动转换行尾
git config --global core.autocrlf true

# macOS/Linux 用户:保持 Unix 行尾
git config --global core.autocrlf input

# 禁用自动转换(如果需要)
git config --global core.autocrlf false

下一步学习

恭喜你完成了 Git 的安装和配置!接下来可以学习:

总结

本文详细介绍了 Git 在各大平台上的安装方法:

  1. Linux:使用包管理器或源码编译
  2. macOS:推荐 Homebrew 或 Xcode 工具
  3. Windows:推荐 Git for Windows

安装完成后,别忘了:

  • ✅ 设置用户名和邮箱
  • ✅ 配置默认分支名称
  • ✅ 设置喜欢的文本编辑器
  • ✅ 启用颜色输出
  • ✅ 配置常用别名

现在你已经准备好开始使用 Git 进行版本控制了!🎉