| name | git-advanced |
| description | Git 高级操作 |
| version | 1.0.0 |
| author | terminal-skills |
| tags | ["devops","git","version-control","branch"] |
Git 高级操作
概述
分支策略、rebase、cherry-pick、hooks 等高级 Git 技能。
分支管理
分支操作
git branch feature/new-feature
git checkout -b feature/new-feature
git switch -c feature/new-feature
git checkout main
git switch main
git branch -d feature/merged
git branch -D feature/unmerged
git push origin --delete feature/old
git branch -m old-name new-name
git branch -a
git branch -v
git branch --merged
git branch --no-merged
分支追踪
git branch --set-upstream-to=origin/main main
git push -u origin feature/new
git branch -vv
Rebase 操作
基础 rebase
git checkout feature
git rebase main
git rebase -i HEAD~5
git rebase -i main
git rebase --continue
git rebase --abort
git rebase --skip
合并提交
git rebase -i HEAD~3
git rebase -i HEAD~3
Cherry-pick
git cherry-pick commit-hash
git cherry-pick commit1 commit2 commit3
git cherry-pick start-commit..end-commit
git cherry-pick -n commit-hash
git cherry-pick --continue
git cherry-pick --abort
Stash 暂存
git stash
git stash push -m "work in progress"
git stash -u
git stash --include-untracked
git stash list
git stash pop
git stash apply
git stash apply stash@{2}
git stash show -p stash@{0}
git stash drop stash@{0}
git stash clear
撤销操作
撤销工作区修改
git checkout -- file.txt
git restore file.txt
git checkout -- .
git restore .
撤销暂存区
git reset HEAD file.txt
git restore --staged file.txt
git reset HEAD
撤销提交
git reset --soft HEAD~1
git reset --hard HEAD~1
git revert commit-hash
git revert HEAD
git revert HEAD~3..HEAD
修改提交
git commit --amend
git commit --amend -m "new message"
git commit --amend --no-edit
git add forgotten-file
git commit --amend --no-edit
远程操作
git remote -v
git remote add upstream https://github.com/original/repo.git
git fetch origin
git fetch --all
git pull --rebase origin main
git push --force-with-lease origin feature
Git Hooks
常用 hooks
.git/hooks/
pre-commit
prepare-commit-msg
commit-msg
post-commit
pre-push
pre-commit 示例
#!/bin/bash
npm run lint
if [ $? -ne 0 ]; then
echo "Lint failed. Commit aborted."
exit 1
fi
npm test
if [ $? -ne 0 ]; then
echo "Tests failed. Commit aborted."
exit 1
fi
exit 0
commit-msg 示例
#!/bin/bash
commit_msg=$(cat "$1")
if ! echo "$commit_msg" | grep -qE "^(feat|fix|docs|style|refactor|test|chore)(\(.+\))?: .{1,50}"; then
echo "Invalid commit message format."
echo "Format: type(scope): message"
exit 1
fi
exit 0
常见场景
场景 1:同步 fork 仓库
git remote add upstream https://github.com/original/repo.git
git fetch upstream
git checkout main
git merge upstream/main
git push origin main
场景 2:清理历史中的大文件
git filter-repo --path large-file.zip --invert-paths
bfg --delete-files large-file.zip
git reflog expire --expire=now --all
git gc --prune=now --aggressive
场景 3:二分查找 bug
git bisect start
git bisect bad
git bisect good v1.0.0
git bisect good
git bisect reset
场景 4:子模块管理
git submodule add https://github.com/user/repo.git path/to/submodule
git submodule init
git submodule update
git clone --recursive https://github.com/user/repo.git
git submodule update --remote
故障排查
| 问题 | 解决方法 |
|---|
| 合并冲突 | 手动解决后 git add + git commit |
| rebase 冲突 | 解决后 git rebase --continue |
| 误删分支 | git reflog 找回 |
| 推送被拒绝 | git pull --rebase 后重试 |
git reflog
git checkout -b recovery commit-hash