文章目录
Git 简介
Git 是一个分布式版本控制系统,由 Linus Torvalds 在 2005 年创建。它具有以下特点:
- 分布式:每个开发者都有完整的代码历史
- 高性能:快速的分支创建和合并
- 数据完整性:使用 SHA-1 哈希确保数据完整性
- 非线性开发:支持复杂的分支和合并策略
Git vs 其他版本控制系统
| 特性 | Git | SVN | CVS |
|---|---|---|---|
| 分布式 | ✅ | ❌ | ❌ |
| 离线工作 | ✅ | ❌ | ❌ |
| 分支成本 | 低 | 高 | 高 |
| 合并能力 | 强 | 中 | 弱 |
| 性能 | 高 | 中 | 低 |
基础配置
bash
# 设置用户信息(必需)
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# 设置默认编辑器
git config --global core.editor "code --wait" # VS Code
git config --global core.editor "vim" # Vim
git config --global core.editor "nano" # Nano
# 设置默认分支名
git config --global init.defaultBranch main
# 设置行尾处理(Windows 推荐)
git config --global core.autocrlf true
# 设置行尾处理(macOS/Linux 推荐)
git config --global core.autocrlf input
# 启用颜色输出
git config --global color.ui auto
# 设置别名
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.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual '!gitk'
# 查看配置
git config --list
git config --global --list
git config user.name
基础概念
Git 的三个区域
工作区 (Working Directory)
↓ git add
暂存区 (Staging Area/Index)
↓ git commit
版本库 (Repository)
文件状态
未跟踪 (Untracked) ──git add──→ 已暂存 (Staged)
↓ git commit
已跟踪 (Tracked) ←──────────────── 已提交 (Committed)
↓ 修改文件 ↑
已修改 (Modified) ──git add──────────┘
Git 对象类型
- Blob:文件内容
- Tree:目录结构
- Commit:提交信息
- Tag:标签引用
仓库操作
创建仓库
bash
# 初始化新仓库
git init
git init my-project
git init --bare # 创建裸仓库(服务器用)
# 克隆现有仓库
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git my-folder
git clone --depth 1 https://github.com/user/repo.git # 浅克隆
git clone --branch develop https://github.com/user/repo.git # 克隆特定分支
仓库信息
bash
# 查看仓库状态
git status
git status -s # 简短格式
git status --porcelain # 机器可读格式
# 查看提交历史
git log
git log --oneline # 单行显示
git log --graph # 图形化显示
git log --stat # 显示统计信息
git log --patch # 显示详细差异
git log -n 5 # 显示最近 5 次提交
git log --since="2 weeks ago" # 时间过滤
git log --author="John" # 作者过滤
git log --grep="fix" # 提交信息过滤
# 查看文件历史
git log filename
git log -p filename # 显示文件的详细变更历史
# 查看分支图
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
文件操作
添加文件到暂存区
bash
# 添加单个文件
git add filename.txt
# 添加多个文件
git add file1.txt file2.txt
# 添加所有文件
git add .
git add -A
git add --all
# 添加所有 .txt 文件
git add *.txt
# 交互式添加
git add -i
git add -p # 部分添加(patch 模式)
# 添加目录
git add directory/
# 强制添加被忽略的文件
git add -f ignored-file.txt
提交更改
bash
# 基本提交
git commit -m "提交信息"
# 详细提交信息
git commit # 打开编辑器
# 跳过暂存区直接提交
git commit -a -m "提交信息"
git commit -am "提交信息"
# 修改最后一次提交
git commit --amend
git commit --amend -m "新的提交信息"
git commit --amend --no-edit # 不修改提交信息
# 空提交(用于触发 CI/CD)
git commit --allow-empty -m "Empty commit"
# 提交时签名
git commit -S -m "Signed commit"
查看差异
bash
# 查看工作区与暂存区的差异
git diff
# 查看暂存区与最后一次提交的差异
git diff --staged
git diff --cached
# 查看工作区与最后一次提交的差异
git diff HEAD
# 查看两个提交之间的差异
git diff commit1 commit2
git diff HEAD~1 HEAD
# 查看特定文件的差异
git diff filename
git diff --staged filename
# 查看统计信息
git diff --stat
git diff --numstat
# 查看单词级别的差异
git diff --word-diff
删除和移动文件
bash
# 删除文件
git rm filename
git rm -f filename # 强制删除
git rm --cached filename # 从暂存区删除但保留工作区文件
# 删除目录
git rm -r directory/
# 移动/重命名文件
git mv old-name new-name
git mv file.txt directory/
# 等价操作
mv old-name new-name
git rm old-name
git add new-name
分支管理
分支基础操作
bash
# 查看分支
git branch # 本地分支
git branch -r # 远程分支
git branch -a # 所有分支
git branch -v # 显示最后一次提交
git branch -vv # 显示跟踪关系
# 创建分支
git branch new-branch
git branch new-branch commit-hash # 从特定提交创建
# 切换分支
git checkout branch-name
git switch branch-name # Git 2.23+
# 创建并切换分支
git checkout -b new-branch
git switch -c new-branch # Git 2.23+
# 从远程分支创建本地分支
git checkout -b local-branch origin/remote-branch
git switch -c local-branch origin/remote-branch
分支管理
bash
# 重命名分支
git branch -m old-name new-name
git branch -M old-name new-name # 强制重命名
# 删除分支
git branch -d branch-name # 安全删除
git branch -D branch-name # 强制删除
git branch --delete branch-name
# 删除远程分支
git push origin --delete branch-name
git push origin :branch-name
# 设置上游分支
git branch --set-upstream-to=origin/main
git branch -u origin/main
# 查看分支合并状态
git branch --merged # 已合并的分支
git branch --no-merged # 未合并的分支
分支合并
bash
# 合并分支
git merge branch-name
# 快进合并
git merge --ff-only branch-name
# 禁用快进合并
git merge --no-ff branch-name
# 压缩合并
git merge --squash branch-name
# 合并时指定提交信息
git merge -m "Merge message" branch-name
# 中止合并
git merge --abort
远程仓库
远程仓库管理
bash
# 查看远程仓库
git remote
git remote -v # 显示详细信息
# 添加远程仓库
git remote add origin https://github.com/user/repo.git
git remote add upstream https://github.com/original/repo.git
# 修改远程仓库 URL
git remote set-url origin https://github.com/user/new-repo.git
# 重命名远程仓库
git remote rename origin new-origin
# 删除远程仓库
git remote remove origin
git remote rm origin
# 查看远程仓库信息
git remote show origin
推送和拉取
bash
# 推送到远程仓库
git push origin main
git push origin branch-name
git push -u origin main # 设置上游并推送
# 推送所有分支
git push origin --all
# 推送标签
git push origin --tags
git push origin tag-name
# 强制推送(危险操作)
git push --force
git push --force-with-lease # 更安全的强制推送
# 从远程仓库拉取
git pull origin main
git pull # 从跟踪的远程分支拉取
# 拉取但不合并
git fetch origin
git fetch --all
# 拉取并变基
git pull --rebase origin main
跟踪远程分支
bash
# 查看跟踪关系
git branch -vv
# 设置跟踪关系
git branch --set-upstream-to=origin/main main
git branch -u origin/main main
# 推送并设置跟踪
git push -u origin feature-branch
# 删除远程跟踪分支
git branch -dr origin/branch-name
标签管理
创建标签
bash
# 轻量标签
git tag v1.0
git tag v1.0 commit-hash
# 附注标签
git tag -a v1.0 -m "Version 1.0"
git tag -a v1.0 commit-hash -m "Version 1.0"
# 签名标签
git tag -s v1.0 -m "Signed version 1.0"
标签操作
bash
# 查看标签
git tag
git tag -l "v1.*" # 模式匹配
# 查看标签信息
git show v1.0
# 删除标签
git tag -d v1.0
# 删除远程标签
git push origin --delete tag v1.0
git push origin :refs/tags/v1.0
# 推送标签
git push origin v1.0
git push origin --tags
# 检出标签
git checkout v1.0
git checkout -b version1 v1.0 # 基于标签创建分支
总结
Git 是一个功能强大的版本控制系统,掌握其基础用法对于现代软件开发至关重要。本指南仅涵盖基础操作部分,而Git 的学习是一个持续的过程,随着经验的积累,你会发现更多高效的使用方法和高级用法。