| name | git-advanced |
| description | Advanced git operations including complex rebase strategies, interactive staging, commit surgery, and history manipulation. Use when user needs to perform complex git operations like rewriting history or advanced merging. |
Git Advanced Operations Skill
This skill provides comprehensive guidance on advanced git operations, sophisticated rebase strategies, commit surgery techniques, and complex history manipulation for experienced git users.
When to Use
Activate this skill when:
- Performing complex interactive rebases
- Rewriting commit history
- Splitting or combining commits
- Advanced merge strategies
- Cherry-picking across branches
- Commit message editing in history
- Author information changes
- Complex conflict resolution
Interactive Rebase Strategies
Basic Interactive Rebase
git rebase -i HEAD~5
git rebase -i abc123^
git rebase -i main
Rebase Commands
Squashing Commits
git rebase -i HEAD~3
pick abc123 feat: add user authentication
squash def456 fix: resolve login bug
squash ghi789 style: format code
git rebase -i main
Fixup Workflow
git commit --fixup=abc123
git rebase -i --autosquash main
git config --global rebase.autosquash true
git log --oneline -5
git commit --fixup=abc123
git rebase -i --autosquash HEAD~3
Reordering Commits
git rebase -i HEAD~5
pick def456 feat: add database migration
pick abc123 feat: add user model
pick ghi789 feat: add API endpoints
pick abc123 feat: add user model
pick def456 feat: add database migration
pick ghi789 feat: add API endpoints
Splitting Commits
git rebase -i HEAD~3
edit abc123 feat: add user and role features
git reset HEAD^
git add user.go
git commit -m "feat: add user management"
git add role.go
git commit -m "feat: add role management"
git rebase --continue
Editing Old Commits
git rebase -i HEAD~5
edit abc123 feat: add authentication
git add modified-file.go
git commit --amend --no-edit
git commit --amend
git rebase --continue
Commit Surgery
Amending Commits
git add forgotten-file.go
git commit --amend --no-edit
git commit --amend -m "fix: correct typo in feature"
git commit --amend --author="John Doe <john@example.com>"
git commit --amend --date="2024-03-15 10:30:00"
Changing Commit Messages
git commit --amend
git rebase -i HEAD~5
git commit --amend -m "new message" --no-edit
Changing Multiple Authors
git filter-branch --env-filter '
if [ "$GIT_COMMITTER_EMAIL" = "old@example.com" ]; then
export GIT_COMMITTER_NAME="New Name"
export GIT_COMMITTER_EMAIL="new@example.com"
fi
if [ "$GIT_AUTHOR_EMAIL" = "old@example.com" ]; then
export GIT_AUTHOR_NAME="New Name"
export GIT_AUTHOR_EMAIL="new@example.com"
fi
' --tag-name-filter cat -- --branches --tags
git filter-repo --email-callback '
return email.replace(b"old@example.com", b"new@example.com")
'
Removing Files from History
git filter-branch --tree-filter 'rm -f passwords.txt' HEAD
git filter-branch --index-filter 'git rm --cached --ignore-unmatch passwords.txt' HEAD
git filter-repo --path passwords.txt --invert-paths
git filter-repo --strip-blobs-bigger-than 10M
BFG Repo-Cleaner
bfg --delete-files passwords.txt
bfg --strip-blobs-bigger-than 50M
bfg --replace-text passwords.txt
git reflog expire --expire=now --all
git gc --prune=now --aggressive
Advanced Cherry-Picking
Basic Cherry-Pick
git cherry-pick abc123
git cherry-pick abc123 def456 ghi789
git cherry-pick abc123..ghi789
git cherry-pick -n abc123
Cherry-Pick with Conflicts
git cherry-pick abc123
git add resolved-file.go
git cherry-pick --continue
git cherry-pick --abort
git cherry-pick --skip
Cherry-Pick Options
git cherry-pick -e abc123
git cherry-pick -s abc123
git cherry-pick --ff abc123
git cherry-pick -n abc123
git commit --author="New Author <new@example.com>"
Mainline Selection for Merge Commits
git cherry-pick -m 1 abc123
git log --graph --oneline
git cherry-pick -m 1 abc123
Advanced Merging
Merge Strategies
git merge -s recursive branch-name
git merge -s ours branch-name
git merge -s theirs branch-name
git merge -s octopus branch1 branch2 branch3
git merge -s subtree branch-name
Merge Strategy Options
git merge -X ours branch-name
git merge -X theirs branch-name
git merge -X ignore-space-change branch-name
git merge -X ignore-all-space branch-name
git merge -X patience branch-name
git merge -X renormalize branch-name
Three-Way Merge
git merge feature-branch
git merge feature-branch -m "Merge feature: add authentication"
git merge --no-ff feature-branch
git merge --ff-only feature-branch
git merge --squash feature-branch
git commit -m "feat: add complete authentication system"
Advanced Conflict Resolution
Understanding Conflict Markers
<<<<<<< HEAD (Current Change)
int result = add(a, b);
=======
int sum = calculate(a, b);
>>>>>>> feature-branch (Incoming Change)
Conflict Resolution Tools
git mergetool
git mergetool --tool=vimdiff
git mergetool --tool=meld
git mergetool --tool=kdiff3
git config --global merge.tool vimdiff
git config --global mergetool.vimdiff.cmd 'vimdiff "$LOCAL" "$MERGED" "$REMOTE"'
git checkout --ours file.go
git checkout --theirs file.go
git checkout --merge file.go
Rerere (Reuse Recorded Resolution)
git config --global rerere.enabled true
git merge feature-branch
git add file.go
git commit
git merge another-branch
git rerere status
git rerere diff
git rerere forget file.go
git rerere clear
Interactive Staging
Partial File Staging
git add -p file.go
Interactive Add
git add -i
Partial Commits
git add -p file.go
git commit -m "feat: add validation logic"
git add file.go
git commit -m "feat: add error handling"
Advanced Reset Operations
Reset Modes
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset HEAD~1
git reset --hard HEAD~1
git reset --hard abc123
Reset vs Revert
git reset --hard HEAD~3
git revert HEAD
git revert HEAD~3
git revert abc123..def456
Advanced Branch Operations
Branch from Specific Commit
git branch new-branch abc123
git checkout new-branch
git checkout -b new-branch abc123
git checkout -b local-branch origin/remote-branch
Orphan Branches
git checkout --orphan new-root
git rm -rf .
echo "# Documentation" > README.md
git add README.md
git commit -m "docs: initialize documentation"
Branch Tracking
git branch -u origin/main
git push -u origin feature-branch
git branch -u origin/develop
git branch -vv
Advanced Stash Operations
Stash Specific Files
git stash push -m "WIP: feature work" file1.go file2.go
git stash push -p
git stash -u
git stash -a
Stash to Branch
git stash branch new-branch stash@{0}
git stash branch feature-work
Partial Stash Application
git checkout stash@{0} -- file.go
git stash apply stash@{0}
git stash pop stash@{0}
Submodule Management
Advanced Submodule Operations
cd submodule-dir
git checkout abc123
cd ..
git add submodule-dir
git commit -m "chore: update submodule to version 1.2.3"
git submodule update --remote --merge
git submodule update --remote --merge path/to/submodule
git submodule foreach 'git checkout main'
git submodule foreach 'git pull'
git clone --recurse-submodules --depth 1 repo-url
Submodule to Subtree Migration
git submodule deinit path/to/submodule
git rm path/to/submodule
rm -rf .git/modules/path/to/submodule
git subtree add --prefix=path/to/submodule \
https://github.com/user/repo.git main --squash
git subtree pull --prefix=path/to/submodule \
https://github.com/user/repo.git main --squash
Advanced Log and History
Custom Log Formatting
git log --pretty=format:"%h - %an, %ar : %s"
git log --pretty=format:"%C(yellow)%h%C(reset) %C(blue)%ad%C(reset) %C(green)%an%C(reset) %s" --date=short
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
Finding Lost Commits
git log --all --oneline --no-walk --decorate $(git fsck --no-reflog | grep commit | cut -d' ' -f3)
git fsck --lost-found
git log --all --grep="search term"
git log --author="John Doe"
git log -S "function_name"
git log -G "regex_pattern"
Bisect Automation
git bisect start
git bisect bad HEAD
git bisect good abc123
git bisect run ./test.sh
make && make test
exit $?
Best Practices
- Backup Before History Rewriting: Create backup branch before complex operations
- Never Rewrite Published History: Only rewrite local commits
- Communicate Rebases: Inform team when force-pushing
- Use Descriptive Commit Messages: Even during interactive rebase
- Test After Rebase: Ensure code still works after history changes
- Prefer Rebase for Local Branches: Keep history linear
- Use Merge for Shared Branches: Preserve complete history
- Sign Important Commits: Use GPG signing for releases
- Document Complex Operations: Leave comments for future reference
- Know When to Stop: Sometimes merge commits are clearer than rebased history
Resources
Additional guides and examples are available in the assets/ directory:
examples/ - Complex rebase and merge scenarios
scripts/ - Automation scripts for common operations
workflows/ - Advanced workflow patterns
See references/ directory for:
- Git internals documentation
- Advanced rebasing strategies
- Filter-branch alternatives
- Conflict resolution techniques