Git 是现代软件开发不可或缺的工具,但大多数开发者只使用了其基础功能。掌握 Git 的高级技巧可以显著提升开发效率和代码管理质量。
Git 基础配置优化#
1. 全局配置#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| # 设置用户信息
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# 设置默认分支名
git config --global init.defaultBranch main
# 设置编辑器
git config --global core.editor "code --wait"
# 设置合并策略
git config --global pull.rebase false
# 设置凭证存储
git config --global credential.helper store
# 设置自动纠错
git config --global help.autocorrect 1
# 设置着色
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.graph 'log --oneline --graph --decorate --all'
|
2. 项目级配置#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| # 在项目根目录创建 .gitignore
cat > .gitignore << EOF
# Dependencies
node_modules/
npm-debug.log*
# Build outputs
dist/
build/
# IDE files
.vscode/
.idea/
*.swp
*.swo
# Environment files
.env
.env.local
# Logs
logs/
*.log
EOF
# 设置项目特定的配置
git config core.fileMode false # 忽略文件权限变化
git config core.autocrlf input # Windows 换行符处理
|
高级分支管理#
1. 功能分支工作流#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # 创建功能分支
git checkout -b feature/user-authentication
# 定期同步主分支
git fetch origin
git rebase origin/main
# 完成功能后合并
git checkout main
git merge feature/user-authentication --no-ff
# 删除已合并的分支
git branch -d feature/user-authentication
git push origin --delete feature/user-authentication
|
2. 分支策略#
1
2
3
4
5
6
7
8
9
| # 查看所有分支及其最后提交时间
git for-each-ref --sort='-authordate:iso8601' --format=' %(authordate:iso8601) %09 %(refname:short)' refs/heads/
# 查看已合并和未合并的分支
git branch --merged
git branch --no-merged
# 安全删除已合并的分支
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
|
3. 分支保护#
1
2
3
4
5
6
7
| # 保护 main 分支
git config --global branch.main.pushRemote origin
git config --global push.default current
# 拒绝强制推送到受保护分支
git config --global receive.denyDeleteCurrent true
git config --global receive.denyNonFastForwards true
|
高级提交技巧#
1. 交互式 Rebase#
1
2
3
4
5
6
7
8
9
10
11
12
13
| # 最近3个提交的交互式 rebase
git rebase -i HEAD~3
# 或指定范围
git rebase -i start_commit_hash..end_commit_hash
# Rebase 命令说明:
# pick: 使用该提交
# reword: 修改提交信息
# edit: 编辑提交
# squash: 合并到上一个提交
# fixup: 类似 squash,但丢弃提交信息
# drop: 删除提交
|
2. 提交信息优化#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| # 使用模板编辑提交信息
git commit -t commit_template.txt
# 提交信息模板示例
cat > commit_template.txt << EOF
# 类型(范围): 简短描述
#
# 详细描述(可选)
#
# 相关 Issue: #123
#
# 类型说明:
# feat: 新功能
# fix: 修复 bug
# docs: 文档更新
# style: 代码格式调整
# refactor: 重构代码
# test: 测试相关
# chore: 构建工具或辅助工具的变动
EOF
# 使用该模板
git config --global commit.template commit_template.txt
|
3. 提交历史清理#
1
2
3
4
5
6
7
8
9
| # 修改最后一次提交
git commit --amend
# 修改最后一次提交信息
git commit --amend -m "新的提交信息"
# 添加文件到最后一次提交
git add .
git commit --amend --no-edit
|
高级合并策略#
1. 合并冲突解决#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| # 查看冲突文件
git status
# 使用工具解决冲突
git mergetool
# 手动解决冲突标记:
# <<<<<<< HEAD
# 当前分支的内容
# =======
# 合并分支的内容
# >>>>>>> branch_name
# 标记冲突已解决
git add 冲突文件名
# 继续合并
git merge --continue
# 取消合并
git merge --abort
|
2. Cherry-pick#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| # 选择单个提交应用到当前分支
git cherry-pick commit_hash
# 选择多个提交
git cherry-pick start_hash..end_hash
# 不包含开始提交
git cherry-pick start_hash^..end_hash
# 仅复制更改,不创建提交
git cherry-pick --no-commit commit_hash
# 解决冲突后继续
git cherry-pick --continue
# 放弃 cherry-pick
git cherry-pick --abort
|
3. 合并策略选择#
1
2
3
4
5
6
7
8
9
10
11
12
13
| # 快进合并(默认)
git merge feature-branch
# 非快进合并(保留分支历史)
git merge --no-ff feature-branch
# Squash 合并(将分支提交合并为一个)
git merge --squash feature-branch
# 策略说明:
# --ff: 快进合并(默认)
# --no-ff: 非快进合并,保留分支历史
# --squash: 将所有提交压缩为单个提交
|
暂存和储藏#
1. 高级 Stash#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| # 创建带信息的储藏
git stash save "实现用户登录功能"
# 暂存未跟踪的文件
git stash -u
# 暂存包括忽略的文件
git stash -a
# 查看储藏列表
git stash list
# 应用储藏
git stash apply stash@{0}
# 应用并删除储藏
git stash pop
# 应用特定储藏
git stash apply 2
# 创建分支从储藏
git stash branch feature-branch stash@{1}
# 查看储藏内容
git stash show -p stash@{0}
# 删除储藏
git stash drop stash@{0}
# 清空所有储藏
git stash clear
|
2. 暂存部分文件#
1
2
3
4
5
6
7
8
9
10
11
| # 交互式暂存
git add -i
# 暂存文件的部分内容
git add -p filename
# 暂存多个文件的部分内容
git add -p
# 暂存编辑器模式
git add -e
|
历史查询和分析#
1. 高级 Log 查看#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| # 图形化查看历史
git log --oneline --graph --all --decorate
# 查看每个文件的修改历史
git log -- filename
# 查看提交的文件变更
git log --stat
# 查看提交的详细差异
git log -p
# 查找特定作者的提交
git log --author="作者名"
# 按时间范围查看
git log --since="2024-01-01" --until="2024-12-31"
# 查找包含特定内容的提交
git log -S "搜索内容"
# 查找修改特定文件的提交
git log --follow filename
# 短格式查看
git log --pretty=format:"%h - %an, %ar : %s"
|
2. 文件历史追踪#
1
2
3
4
5
6
7
8
9
10
11
| # 查看文件的修改者和修改时间
git blame filename
# 忽略空白字符的 blame
git blame -w filename
# 显示原始行号
git blame -L 1,10 filename
# 查看文件每一行的最后修改
git log -p -- filename
|
3. 提交统计#
1
2
3
4
5
6
7
8
| # 查看开发者贡献统计
git shortlog -sn
# 查看文件修改次数
git log --name-only | sort | uniq -c | sort -rg
# 查看项目统计
git diff --stat HEAD~5 HEAD
|
高级重置操作#
1. Reset 命令详解#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # 软重置(保留工作区和暂存区)
git reset --soft HEAD~1
# 混合重置(保留工作区,清空暂存区)
git reset HEAD~1
# 硬重置(清空工作区和暂存区)
git reset --hard HEAD~1
# 重置到指定提交
git reset --hard commit_hash
# 重置文件到特定提交
git reset commit_hash -- filename
|
2. Revert 操作#
1
2
3
4
5
6
7
8
9
10
11
| # 撤销单个提交
git revert commit_hash
# 撤销多个提交
git revert start_hash..end_hash
# 不创建新提交,直接修改工作区
git revert --no-commit commit_hash
# 编辑 revert 信息
git revert --edit commit_hash
|
远程仓库管理#
1. 多远程仓库#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| # 查看所有远程仓库
git remote -v
# 添加远程仓库
git remote add upstream https://github.com/original/repo.git
# 重命名远程仓库
git remote rename origin old-origin
# 删除远程仓库
git remote remove upstream
# 获取特定远程分支
git fetch origin main:main
# 推送所有分支
git push --all origin
# 删除远程分支
git push origin --delete feature-branch
|
2. 同步 Fork#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| # 添加上游仓库
git remote add upstream https://github.com/original/repo.git
# 获取上游更新
git fetch upstream
# 合并上游更新
git checkout main
git merge upstream/main
# 或使用 rebase
git rebase upstream/main
# 推送到自己的仓库
git push origin main
|
性能优化#
1. 仓库优化#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # 垃圾回收
git gc
# 激进的垃圾回收
git gc --aggressive --prune=now
# 清理无用文件
git clean -fd
# 检查仓库完整性
git fsck
# 重新打包数据库
git repack -a -d --depth=250 --window=250
|
2. 大文件处理#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| # 使用 Git LFS(Large File Storage)
git lfs install
# 跟踪大文件类型
git lfs track "*.psd"
git lfs track "*.zip"
# 查看跟踪的文件类型
git lfs track
# 添加 .gitattributes
git add .gitattributes
# 推送 LFS 文件
git push origin main --push-option=ssh://git@github.com/user/repo.git
|
自动化和脚本#
1. Git Hooks#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| # 安装示例 hooks
cp /usr/share/git-core/templates/hooks/* .git/hooks/
# Pre-commit hook 示例
cat > .git/hooks/pre-commit << 'EOF'
#!/bin/sh
# 运行代码检查
npm run lint
# 运行测试
npm test
EOF
chmod +x .git/hooks/pre-commit
# Post-commit hook 示例
cat > .git/hooks/post-commit << 'EOF'
#!/bin/sh
# 提交后执行
echo "提交完成,请推送到远程仓库"
EOF
chmod +x .git/hooks/post-commit
|
2. 实用脚本#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| # 快速提交脚本
cat > quick-commit.sh << 'EOF'
#!/bin/bash
if [ $# -eq 0 ]; then
echo "请提供提交信息"
exit 1
fi
git add .
git commit -m "$1"
git push
EOF
chmod +x quick-commit.sh
# 分支清理脚本
cat > cleanup-branches.sh << 'EOF'
#!/bin/bash
git branch --merged | grep -v "\*" | grep -v "main" | xargs -n 1 git branch -d
echo "已清理已合并的分支"
EOF
chmod +x cleanup-branches.sh
|
故障排除#
1. 常见问题解决#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| # 撤销已推送的提交
git revert HEAD
git push origin main
# 强制推送(谨慎使用)
git push --force-with-lease origin feature-branch
# 找回丢失的提交
git reflog
git checkout commit_hash
# 解决 detached HEAD 状态
git checkout main
git merge lost_commit
# 重置到远程状态
git fetch origin
git reset --hard origin/main
|
2. 恢复操作#
1
2
3
4
5
6
7
8
9
10
11
12
| # 恢复删除的文件
git checkout HEAD -- filename
# 恢复删除的分支
git reflog
git checkout -b branch_name commit_hash
# 查看所有引用
git show-ref
# 查找包含特定提交的分支
git branch --contains commit_hash
|
Git 高级技巧能够显著提升开发效率:
- 配置优化:合理设置全局和项目配置
- 分支管理:采用清晰的分支策略
- 提交质量:使用交互式 rebase 优化提交历史
- 冲突处理:掌握各种合并策略和冲突解决技巧
- 历史管理:高效查询和分析代码历史
- 性能优化:定期维护和优化仓库性能
- 自动化:利用 hooks 和脚本提高效率
掌握这些技巧将帮助你更专业地使用 Git,提升团队协作效率。
🧰 本章配套工具箱#
边读边练:以下工具均在浏览器本地运行,数据不上传,可直接打开使用。