Git 版本控制进阶技巧
Git 是强大的分布式版本控制系统,掌握进阶技巧可以大幅提升开发效率。
分支管理策略
Git Flow 工作流
git checkout -b develop
git checkout -b feature/user-auth git add . git commit -m "feat: implement user authentication"
git checkout develop git merge feature/user-auth git branch -d feature/user-auth
git checkout -b release/v1.0.0 git commit -m "chore: prepare for release"
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
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 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
|
钩子配置
本地钩子
echo '#!/bin/sh npm run lint npm run test ' > .git/hooks/pre-commit chmod +x .git/hooks/pre-commit
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 管理钩子
{ "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
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 git config core.autocrlf true
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}
git checkout <commit-hash> git checkout main
git clean -fd git clean -n
|
[!tip]
- 定期进行 git gc 优化仓库性能
- 使用 .gitignore 避免跟踪无用文件
- 提交前检查
git status 确认更改 - 使用有意义的提交信息格式
参考资料