| name | git-advanced |
| description | Advanced git workflows including worktrees, bisect, interactive rebase, hooks, and recovery techniques |
Git Advanced
Worktrees
git worktree add ../feature-auth feature/auth
git worktree add ../hotfix-123 -b hotfix/123 origin/main
git worktree list
git worktree remove ../feature-auth
Worktrees let you work on multiple branches simultaneously without stashing or committing WIP. Each worktree has its own working directory but shares the same .git repository.
Bisect
git bisect start
git bisect bad HEAD
git bisect good v1.5.0
git bisect good
git bisect bad
git bisect start HEAD v1.5.0
git bisect run npm test
git bisect reset
Bisect performs binary search across commits to find which commit introduced a bug. Automated bisect with run is the fastest approach.
Interactive Rebase
git rebase -i HEAD~5
git fetch origin
git rebase origin/main
git rebase --continue
git rebase --abort
Git Hooks
#!/bin/sh
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
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
npm test
if [ $? -ne 0 ]; then
echo "Tests failed. Push aborted."
exit 1
fi
Recovery Techniques
git reset --soft HEAD~1
git reflog
git checkout -b recovered-branch HEAD@{3}
git checkout abc1234 -- path/to/file.ts
git fsck --lost-found
git show <dangling-commit-sha>
git reflog
git reset --hard HEAD@{5}
Useful Aliases
[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
Anti-Patterns
- Force-pushing to shared branches without
--force-with-lease
- Rebasing commits that have already been pushed and shared
- Committing large binary files without Git LFS
- Using
git add . without reviewing git diff --staged
- Not using
.gitignore for build artifacts, dependencies, and secrets
- Keeping long-lived feature branches instead of merging frequently
Checklist