𝑻𝒆𝒏𝑪𝒍𝒂𝒘正在头脑风暴···
𝑻𝒆𝒏𝑲𝒊𝑺𝒆𝒀𝒂の𝑨𝒈𝒆𝒏𝒕助手
𝑻𝒆𝒏-𝒇𝒍𝒂𝒔𝒉

Git 版本控制进阶技巧

Git 是强大的分布式版本控制系统,掌握进阶技巧可以大幅提升开发效率。

分支管理策略

Git Flow 工作流

# 创建 develop 分支
git checkout -b develop

# 创建功能分支
git checkout -b feature/user-auth
git add .
git commit -m "feat: implement user authentication"

# 合并到 develop
git checkout develop
git merge feature/user-auth
git branch -d feature/user-auth

# 创建 release 分支
git checkout -b release/v1.0.0
git commit -m "chore: prepare for release"

# 合并到 main 和 develop
git checkout main
git merge release/v1.0.0
git tag v1.0.0

git checkout develop
git merge release/v1.0.0
git branch -d release/v1.0.0

GitHub Flow

# 简化流程
git checkout -b feature/new-feature
git add .
git commit -m "feat: add new feature"
git push origin feature/new-feature

# 创建 Pull Request,审核后合并
git checkout main
git pull origin main
git branch -d feature/new-feature

Trunk Based Development

# 频繁合并到主干
git checkout main
git checkout -b feature/small-change
git add .
git commit -m "fix: small bug fix"
git checkout main
git merge feature/small-change
git push origin main

高级命令

交互式 Rebase

# 清理提交历史
git rebase -i HEAD~5

# 可选操作:
# pick: 使用提交
# reword: 修改提交信息
# edit: 修改提交内容
# squash: 合并到前一个提交
# fixup: 合并到前一个提交(丢弃提交信息)
# exec: 执行命令
# drop: 丢弃提交

撤销操作

# 修改最后一次提交
git commit --amend

# 修改最后一次提交信息
git commit --amend -m "new commit message"

# 撤销工作区修改
git checkout -- file.js
git restore file.js

# 撤销暂存区修改
git reset HEAD file.js
git restore --staged file.js

# 完全回退到指定提交
git reset --hard <commit-hash>

# 保留文件修改,回退到指定提交
git reset --soft <commit-hash>

# 创建反向提交
git revert <commit-hash>

暂存工作

# 暂存当前工作
git stash push -m "work in progress"

# 查看暂存列表
git stash list

# 应用最近的暂存
git stash pop

# 应用指定暂存(不删除)
git stash apply stash@{1}

# 删除暂存
git stash drop stash@{1}

# 清空所有暂存
git stash clear

历史操作

查看历史

# 基本日志
git log
git log --oneline
git log --graph --oneline

# 详细信息
git log -p --stat
git log --show-signature

# 过滤提交
git log --author="John Doe"
git log --grep="fix bug"
git log --since="2024-01-01" --until="2024-12-31"

# 查看特定文件历史
git log --follow file.js
git log -p file.js

# 查看合并提交
git log --merges
git log --first-parent

Blame 和搜索

# 查看文件修改历史
git blame file.js

# 搜索代码
git grep "function"
git grep -n "function" # 显示行号

# 搜索提交内容
git log -S "functionName"
git log -G "regex"

# 搜索提交信息
git log --grep="fix"
git log --all-match --grep="fix" --grep="bug"

远程操作

远程分支管理

# 查看远程分支
git branch -r
git remote show origin

# 跟踪远程分支
git checkout -b feature origin/feature
git branch --set-upstream-to=origin/develop develop

# 推送新分支
git push -u origin feature-branch

# 删除远程分支
git push origin --delete feature-branch
git push origin :feature-branch

# 同步远程分支
git remote prune origin
git fetch --prune

高级推送

# 强制推送(谨慎使用)
git push --force-with-lease
git push origin main --force-with-lease

# 推送标签
git push --tags
git push origin v1.0.0

# 同时推送分支和标签
git push --follow-tags

# 删除远程标签
git push origin --delete v1.0.0

子模块管理

添加和管理子模块

# 添加子模块
git submodule add https://github.com/user/repo.git libs/repo

# 初始化子模块
git submodule init
git submodule update

# 克隆包含子模块的项目
git clone --recursive https://github.com/user/project

# 更新子模块
git submodule update --remote
git submodule foreach git pull origin main

# 删除子模块
git submodule deinit libs/repo
rm -rf .git/modules/libs/repo
git rm libs/repo

钩子配置

本地钩子

# pre-commit 钩子
echo '#!/bin/sh
npm run lint
npm run test
' > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

# commit-msg 钩子
echo '#!/bin/sh
commit_regex="^(feat|fix|docs|style|refactor|test|chore)(\(.+\))?: .+"
if ! grep -qE "$commit_regex" "$1"; then
echo "Invalid commit message format"
exit 1
fi
' > .git/hooks/commit-msg
chmod +x .git/hooks/commit-msg

使用 Husky 管理钩子

// package.json
{
"husky": {
"hooks": {
"pre-commit": "lint-staged",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
"pre-push": "npm run test"
}
},
"lint-staged": {
"*.js": ["eslint --fix", "git add"]
}
}

性能优化

仓库优化

# 清理无用文件
git gc --prune=now

# 打包历史
git repack -a -d --depth=50 --window=50

# 清理大文件
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch large-file.zip' \
--prune-empty --tag-name-filter cat -- --all

# 使用 BFG 工具清理大文件
java -jar bfg.jar --strip-blobs-bigger-than 100M
git gc --prune=now

浅克隆

# 浅克隆(只获取最新提交)
git clone --depth 1 https://github.com/user/repo

# 指定深度克隆
git clone --depth 5 https://github.com/user/repo

# 指定时间范围克隆
git clone --shallow-since="2024-01-01" https://github.com/user/repo

# 单分支克隆
git clone --single-branch --branch main https://github.com/user/repo

工作树管理

多工作树

# 创建新的工作树
git worktree add ../feature-branch feature-branch

# 列出工作树
git worktree list

# 删除工作树
git worktree remove ../feature-branch

# 清理工作树
git worktree prune

配置优化

全局配置

# 用户信息
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# 编辑器
git config --global core.editor "code --wait"

# 默认分支名
git config --global init.defaultBranch main

# 推送策略
git config --global push.default simple

# 拉取策略
git config --global pull.rebase true

# 别名
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm commit
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.graph 'log --oneline --graph --decorate --all'

项目级配置

# 忽略文件权限变更
git config core.filemode false

# 换行符处理
git config core.autocrlf input # Linux/Mac
git config core.autocrlf true # Windows

# 大文件处理
git config core.largefile.threshold 1M

故障排除

常见问题

# 解决合并冲突
git checkout --theirs file.js
git checkout --ours file.js
git add file.js
git commit

# 恢复删除的文件
git checkout HEAD~1 -- deleted-file.js

# 查找丢失的提交
git reflog
git checkout HEAD@{2}

# 分离 HEAD 状态
git checkout <commit-hash>
git checkout main

# 清理未跟踪文件
git clean -fd
git clean -n # 预览

[!tip]

  • 定期进行 git gc 优化仓库性能
  • 使用 .gitignore 避免跟踪无用文件
  • 提交前检查 git status 确认更改
  • 使用有意义的提交信息格式

参考资料