关于 git-advanced
面向需要使用高级仓库操作的开发者,介绍 worktree、bisect、交互式 rebase、Git hooks、恢复方法及操作防护。
主要功能
- 01使用 worktree 并行处理分支
- 02通过 bisect 定位提交
- 03交互式 rebase 操作
- 04用于 lint 和检查的 Git hooks
- 05使用 reset 和 reflog 恢复
适用场景
- 01不暂存改动而同时处理多个分支
- 02找出引入缺陷的提交
- 03合并前整理提交
- 04恢复已删除的分支或文件
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ rohitg00/ awesome-claude-code-toolkit --skill git-advanced安装后在 Claude Code 中输入/git-advanced确认已加载。
详细介绍
Git 高级技巧
工作树
# Create a worktree for a feature branch (avoids stashing)
git worktree add ../feature-auth feature/auth
# Create a worktree with a new branch
git worktree add ../hotfix-123 -b hotfix/123 origin/main
# List all worktrees
git worktree list
# Remove a worktree after merging
git worktree remove ../feature-auth
工作树让你无需暂存或提交 WIP,即可同时在多个分支上工作。每个工作树都有自己的工作目录,但共享同一个 .git 仓库。
二分查找
# Start bisect, mark current as bad and known good commit
git bisect start
git bisect bad HEAD
git bisect good v1.5.0
# Git checks out a midpoint commit. Test it, then mark:
git bisect good # if this commit works
git bisect bad # if this commit is broken
# Automate with a test script
git bisect start HEAD v1.5.0
git bisect run npm test
# When done, reset
git bisect reset
二分查找会在各个提交之间执行二分搜索,以找出引入 Bug 的提交。使用 run 自动执行二分查找是最快的方法。
交互式变基
# Rebase last 5 commits interactively
git rebase -i HEAD~5
# Common operations in the editor:
# pick - keep commit as-is
# reword - change commit message
# edit - stop to amend the commit
# squash - merge into previous commit, keep both messages
# fixup - merge into previous commit, discard this message
# drop - remove the commit entirely
# Rebase feature branch onto latest main
git fetch origin
git rebase origin/main
# Continue after resolving conflicts
git rebase --continue
# Abort if things go wrong
git rebase --abort
Git 钩子
#!/bin/sh
# .git/hooks/pre-commit
# Run linter on staged files only
STAGED_FILES=$(git diff --cached --name-only --diff-filter=d | grep -E '\.(ts|tsx|js|jsx)$')
if [ -n "$STAGED_FILES" ]; then
npx eslint $STAGED_FILES --fix
git add $STAGED_FILES
fi
#!/bin/sh
# .git/hooks/commit-msg
# Enforce conventional commit format
COMMIT_MSG=$(cat "$1")
PATTERN="^(feat|fix|docs|style|refactor|test|chore)(\(.+\))?: .{1,72}$"
if ! echo "$COMMIT_MSG" | head -1 | grep -qE "$PATTERN"; then
echo "Error: Commit message must follow Conventional Commits format"
echo "Example: feat(auth): add OAuth2 login flow"
exit 1
fi
#!/bin/sh
# .git/hooks/pre-push
# Run tests before pushing
npm test
if [ $? -ne 0 ]; then
echo "Tests failed. Push aborted."
exit 1
fi
恢复技巧
# Undo last commit but keep changes staged
git reset --soft HEAD~1
# Recover a deleted branch using reflog
git reflog
git checkout -b recovered-branch HEAD@{3}
# Recover a file from a specific commit
git checkout abc1234 -- path/to/file.ts
# Find lost commits (dangling after reset or rebase)
git fsck --lost-found
git show <dangling-commit-sha>
# Undo a rebase
git reflog
git reset --hard HEAD@{5} # point before rebase started
实用别名
# ~/.gitconfig
[alias]
lg = log --graph --oneline --decorate --all
st = status -sb
co = checkout
unstage = reset HEAD --
last = log -1 HEAD --stat
branches = branch -a --sort=-committerdate
stash-all = stash push --include-untracked
conflicts = diff --name-only --diff-filter=U
反模式
- 在共享分支上强制推送,但未使用
--force-with-lease - 对已经推送并共享的提交进行变基
- 未使用 Git LFS 就提交大型二进制文件
- 使用
git add .,但未检查git diff --staged - 未使用
.gitignore忽略构建产物、依赖项和机密信息 - 长期保留功能分支,而不是频繁合并
检查清单
- 使用工作树并行处理不同分支,而不是暂存工作
- 使用
git bisect run和测试命令自动查找 Bug - 合并到 main 之前,使用交互式变基整理提交
- 提交前钩子会对已暂存文件运行 lint
- 通过 commit-msg 钩子强制执行提交信息格式
- 强制推送时使用
--force-with-lease,而不是--force - 执行任何破坏性操作前先查看 reflog
-
.gitignore涵盖构建输出、依赖项和环境文件
评论
支持 Markdown