Claude
Skills
Sign in
Back

git-advanced

Included with Lifetime
$97 forever

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.

General

What this skill does


# 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

```bash
# Rebase last 5 commits
git rebase -i HEAD~5

# Rebase from specific commit
git rebase -i abc123^

# Rebase entire branch
git rebase -i main
```

### Rebase Commands

```bash
# Interactive rebase editor commands:
# p, pick = use commit
# r, reword = use commit, but edit commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like squash, but discard commit message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
```

### Squashing Commits

```bash
# Example: Squash last 3 commits
git rebase -i HEAD~3

# In editor:
pick abc123 feat: add user authentication
squash def456 fix: resolve login bug
squash ghi789 style: format code

# Squash all commits in feature branch
git rebase -i main
# Mark all except first as 'squash'
```

### Fixup Workflow

```bash
# Create fixup commit automatically
git commit --fixup=abc123

# Autosquash during rebase
git rebase -i --autosquash main

# Set autosquash as default
git config --global rebase.autosquash true

# Example workflow:
git log --oneline -5
# abc123 feat: add authentication
# def456 feat: add authorization
git commit --fixup=abc123
git rebase -i --autosquash HEAD~3
```

### Reordering Commits

```bash
# Interactive rebase
git rebase -i HEAD~5

# In editor, change order:
pick def456 feat: add database migration
pick abc123 feat: add user model
pick ghi789 feat: add API endpoints

# Reorder by moving lines:
pick abc123 feat: add user model
pick def456 feat: add database migration
pick ghi789 feat: add API endpoints
```

### Splitting Commits

```bash
# Start interactive rebase
git rebase -i HEAD~3

# Mark commit to split with 'edit'
edit abc123 feat: add user and role features

# When rebase stops:
git reset HEAD^

# Stage and commit parts separately
git add user.go
git commit -m "feat: add user management"

git add role.go
git commit -m "feat: add role management"

# Continue rebase
git rebase --continue
```

### Editing Old Commits

```bash
# Start interactive rebase
git rebase -i HEAD~5

# Mark commit with 'edit'
edit abc123 feat: add authentication

# When rebase stops, make changes
git add modified-file.go
git commit --amend --no-edit

# Or change commit message
git commit --amend

# Continue rebase
git rebase --continue
```

## Commit Surgery

### Amending Commits

```bash
# Amend last commit (add changes)
git add forgotten-file.go
git commit --amend --no-edit

# Amend commit message
git commit --amend -m "fix: correct typo in feature"

# Amend author information
git commit --amend --author="John Doe <[email protected]>"

# Amend date
git commit --amend --date="2024-03-15 10:30:00"
```

### Changing Commit Messages

```bash
# Change last commit message
git commit --amend

# Change older commit messages
git rebase -i HEAD~5
# Mark commits with 'reword'

# Change commit message without opening editor
git commit --amend -m "new message" --no-edit
```

### Changing Multiple Authors

```bash
# Filter-branch (legacy method, use filter-repo instead)
git filter-branch --env-filter '
if [ "$GIT_COMMITTER_EMAIL" = "[email protected]" ]; then
    export GIT_COMMITTER_NAME="New Name"
    export GIT_COMMITTER_EMAIL="[email protected]"
fi
if [ "$GIT_AUTHOR_EMAIL" = "[email protected]" ]; then
    export GIT_AUTHOR_NAME="New Name"
    export GIT_AUTHOR_EMAIL="[email protected]"
fi
' --tag-name-filter cat -- --branches --tags

# Modern method with git-filter-repo
git filter-repo --email-callback '
    return email.replace(b"[email protected]", b"[email protected]")
'
```

### Removing Files from History

```bash
# Remove file from all history
git filter-branch --tree-filter 'rm -f passwords.txt' HEAD

# Better performance with index-filter
git filter-branch --index-filter 'git rm --cached --ignore-unmatch passwords.txt' HEAD

# Modern method with git-filter-repo (recommended)
git filter-repo --path passwords.txt --invert-paths

# Remove large files
git filter-repo --strip-blobs-bigger-than 10M
```

### BFG Repo-Cleaner

```bash
# Install BFG
# brew install bfg (macOS)
# apt-get install bfg (Ubuntu)

# Remove files by name
bfg --delete-files passwords.txt

# Remove large files
bfg --strip-blobs-bigger-than 50M

# Replace passwords in history
bfg --replace-text passwords.txt

# After BFG cleanup
git reflog expire --expire=now --all
git gc --prune=now --aggressive
```

## Advanced Cherry-Picking

### Basic Cherry-Pick

```bash
# Cherry-pick single commit
git cherry-pick abc123

# Cherry-pick multiple commits
git cherry-pick abc123 def456 ghi789

# Cherry-pick range of commits
git cherry-pick abc123..ghi789

# Cherry-pick without committing (stage only)
git cherry-pick -n abc123
```

### Cherry-Pick with Conflicts

```bash
# When conflicts occur
git cherry-pick abc123
# CONFLICT: resolve conflicts

# After resolving conflicts
git add resolved-file.go
git cherry-pick --continue

# Or abort cherry-pick
git cherry-pick --abort

# Skip current commit
git cherry-pick --skip
```

### Cherry-Pick Options

```bash
# Edit commit message during cherry-pick
git cherry-pick -e abc123

# Sign-off cherry-picked commit
git cherry-pick -s abc123

# Keep original author date
git cherry-pick --ff abc123

# Apply changes without commit attribution
git cherry-pick -n abc123
git commit --author="New Author <[email protected]>"
```

### Mainline Selection for Merge Commits

```bash
# Cherry-pick merge commit (specify parent)
git cherry-pick -m 1 abc123

# -m 1 = use first parent (main branch)
# -m 2 = use second parent (merged branch)

# Example workflow:
git log --graph --oneline
#   *   abc123 Merge pull request #123
#   |\
#   | * def456 feat: feature commit
#   * | ghi789 fix: main branch commit

# To cherry-pick the merge keeping main branch changes:
git cherry-pick -m 1 abc123
```

## Advanced Merging

### Merge Strategies

```bash
# Recursive merge (default)
git merge -s recursive branch-name

# Ours (keep our changes on conflict)
git merge -s ours branch-name

# Theirs (keep their changes on conflict)
git merge -s theirs branch-name

# Octopus (merge 3+ branches)
git merge -s octopus branch1 branch2 branch3

# Subtree merge
git merge -s subtree branch-name
```

### Merge Strategy Options

```bash
# Ours (resolve conflicts with our version)
git merge -X ours branch-name

# Theirs (resolve conflicts with their version)
git merge -X theirs branch-name

# Ignore whitespace
git merge -X ignore-space-change branch-name
git merge -X ignore-all-space branch-name

# Patience algorithm (better conflict detection)
git merge -X patience branch-name

# Renormalize line endings
git merge -X renormalize branch-name
```

### Three-Way Merge

```bash
# Standard three-way merge
git merge feature-branch

# With custom merge message
git merge feature-branch -m "Merge feature: add authentication"

# No fast-forward (always create merge commit)
git merge --no-ff feature-branch

# Fast-forward only (fail if merge commit needed)
git merge --ff-only feature-branch

# Squash merge (combine all commits)
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

```bash
# Use mergetool
git mergetool

# Specify merg

Related in General