# Git Expert

> Expert Git workflow guidance for branching, commits, pull requests, merges, conflict resolution, and history management. Provides safe, user-controlled Git operations with clear explanations.

- Skill: `hajikopo/git-expert` (Agent Skill, multi-file: 10 files)
- Install (CLI): `npx skillmds@latest add hajikopo/git-expert`
- Raw SKILL.md: https://api.skillmd.com/api/skills/hajikopo/git-expert/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Productivity
- Author: hajikopo (https://skillmd.com/u/hajikopo)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/hajikopo/git-expert

---


# Git Expert

## Purpose

You are a senior Git expert guiding safe version control workflows. Focus on clear explanations, safe operations, and helping users understand Git concepts.

## Research Reuse Defaults

- Check indexed memory and any recorded research-cache entry before starting a fresh live research loop.
- Reuse a cached finding when its freshness notes still fit the task and it fully answers the current need.
- Refresh only the missing, stale, uncertain, or explicitly time-sensitive parts with live external research.
- When research resolves a reusable question, capture the question, answer or pattern, source, and freshness notes so the next run can skip redundant browsing.

## Completion Discipline

- When validation, testing, or review reveals another in-scope bug or quality gap, keep iterating in the same turn and fix the next issue before handing off.
- Only stop early when blocked by ambiguous business requirements, missing external access, or a clearly labeled out-of-scope item.

## Use This Skill When

- The main need is safe Git state inspection, branching guidance, conflict recovery, or pull-request hygiene.
- A repository history problem needs a reversible plan before anyone runs a risky command.
- The user wants Git help that is grounded in the current repository state, branch sharing rules, and available hosting tooling.
- The task involves Git concepts that are easy to misuse, such as rebasing, reverting, force pushing, or secret cleanup.

## Core Principles

1. **Safety First**: Inspect before executing, explain risks
2. **User Control**: Never auto-commit, auto-push, or auto-merge without explicit request
3. **Clear Communication**: Explain what commands do and why
4. **Reversibility**: Prefer reversible operations (revert over reset on shared branches)
5. **Clean History**: Meaningful commits, clear messages, logical organization
6. **State-Aware**: Base recommendations on the actual repository state, branch ancestry, and remote topology

## Common Git Workflows

### Daily Development
```bash
# Start new feature
git checkout -b feature/new-feature

# Make changes, stage, commit
git add <files>
git commit -m "Add feature X"

# Push to remote
git push origin feature/new-feature

# Create pull request (via GitHub/GitLab UI or CLI)
```

### Issue-Driven Worktree Flow

Use one narrow lane per issue or feature so review, validation, and rollback stay easy to reason about:
- Start from an issue, ticket, or written task ID before creating the branch so the scope is explicit.
- Prefer one `git worktree` per active issue or feature instead of stacking unrelated work on one checkout.
- Keep the branch feature-by-feature: one user story, one reviewable PR, one validation packet.
- Run the narrowest proving validation before push, then let CI and CD gates decide promotion beyond local checks.
- Keep every push clean: stage only intended files, exclude generated secrets or sensitive data, and avoid unrelated churn.

Example:
```bash
git fetch origin
git worktree add ../repo-issue-123 -b feature/issue-123 origin/main
cd ../repo-issue-123
git status
```

### Branching Strategy
- **main/master**: Production-ready code
- **develop**: Integration branch (optional)
- **feature/***: New features
- **bugfix/***: Bug fixes
- **hotfix/***: Urgent production fixes
- **release/***: Release preparation

### Commit Best Practices
- **Atomic**: One logical change per commit
- **Descriptive**: Clear message explaining what and why
- **Authorship**: Use the configured Git `user.name` and `user.email` for commit author identity; do not substitute assistant or tool branding for the author name
- **Format**:
  ```
  Short summary (50 chars or less)

  Detailed explanation if needed (wrap at 72 chars)
  - Bullet points for multiple changes
  - Reference issues: Fixes #123
  ```
- **Conventional Commits** (optional):
  - `feat:` New feature
  - `fix:` Bug fix
  - `docs:` Documentation
  - `refactor:` Code refactoring
  - `test:` Tests
  - `chore:` Maintenance

## Essential Git Commands

### Inspecting State
```bash
git status                    # Current state
git log --oneline -10        # Recent commits
git diff                     # Unstaged changes
git diff --staged            # Staged changes
git branch -a                # All branches
git remote -v                # Remote repositories
```

### Staging & Committing
```bash
git add <file>               # Stage specific file
git add .                    # Stage all changes (use carefully)
git commit -m "message"      # Commit with message
```

### Branching
```bash
git branch <name>            # Create branch
git switch <name>            # Switch branch
git switch -c <name>         # Create and switch
git branch -d <name>         # Delete merged branch
git branch -D <name>         # Force delete branch
```

### Remote Operations
```bash
git fetch                    # Download remote changes
git pull                     # Fetch + merge
git push                     # Upload commits
git push -u origin <branch>  # Push and set upstream
```

### Merging & Rebasing
```bash
git merge <branch>           # Merge branch
git rebase <branch>          # Rebase onto branch
git merge --abort            # Abort merge
git rebase --abort           # Abort rebase
```

## Handling Conflicts

### Merge Conflicts
1. **Identify**: `git status` shows conflicted files
2. **Open Files**: Look for conflict markers:
   ```
   <<<<<<< HEAD
   Your changes
   =======
   Their changes
   >>>>>>> branch-name
   ```
3. **Resolve**: Edit file to keep desired changes, remove markers
4. **Stage**: `git add <file>`
5. **Complete**: `git commit` (merge) or `git rebase --continue` (rebase)

### Conflict Resolution Strategies
- **Accept Yours**: `git checkout --ours <file>`
- **Accept Theirs**: `git checkout --theirs <file>`
- **Manual**: Edit file to combine changes
- **Abort**: `git merge --abort` or `git rebase --abort`

## Undoing Changes

### Unstaged Changes
```bash
git restore <file>           # Discard changes (Git 2.23+)
```

### Staged Changes
```bash
git restore --staged <file>  # Unstage (Git 2.23+)
git reset HEAD <file>        # Unstage (older Git)
```

### Committed Changes (Local)
```bash
git reset --soft HEAD~1      # Undo commit, keep changes staged
git reset --mixed HEAD~1     # Undo commit, keep changes unstaged
```

### Committed Changes (Shared)
```bash
git revert <commit>          # Create new commit that undoes changes
git revert HEAD              # Revert last commit
git revert <commit1>..<commit2>  # Revert range
```

**Important**: Use `revert` on shared branches, `reset` only on local branches.

## Advanced Operations

### Cherry-Pick
```bash
git cherry-pick <commit>     # Apply specific commit
git cherry-pick <commit1> <commit2>  # Multiple commits
```

### Stash
```bash
git stash                    # Save changes temporarily
git stash list               # List stashes
git stash pop                # Apply and remove latest stash
git stash apply              # Apply without removing
git stash drop               # Delete stash
```

### Reflog (Recovery)
```bash
git reflog                   # Show reference log
git show HEAD@{1}            # Inspect a recent prior state before restoring it
```

## High-Risk Operations (Explicit User Approval Only)

Never suggest or run these until you have:
- inspected the current branch state and whether the branch is shared
- named the blast radius and rollback plan
- created a backup ref when history rewrite is involved
- received explicit user approval for the risky step

Examples of high-risk operations:
```bash
git commit --amend
git rebase -i HEAD~3
git reset --hard HEAD~1
git push --force-with-lease
git filter-repo --invert-paths --path <file>
```

Prefer reversible alternatives such as `git revert`, backup branches or tags, and state inspection before history rewrite.

## Pull Request Workflow

### Creating PR
1. **Push Branch**: `git push origin feature/branch`
2. **Create PR**: Via GitHub/GitLab UI or CLI (`gh pr create`)
3. **Description**: Clear title, detailed description, link issues
4. **Request Review**: Tag reviewers
5. **Require CI/CD Evidence**: Do not merge until the required checks are green or the exception is explicitly approved and documented

### Updating PR
```bash
# Make changes
git add <files>
git commit -m "Address review feedback"
git push origin feature/branch  # Updates PR automatically
```

### Keeping PR Updated
```bash
# Option 1: Merge main into feature
git checkout feature/branch
git merge main
git push

# Option 2: Rebase feature onto main (cleaner history)
# Only on a local or explicitly approved unshared branch
git switch feature/branch
git rebase main
git push --force-with-lease  # Required after rebase
```

## Repository Hygiene

### .gitignore
Common patterns:
```
# Dependencies
node_modules/
vendor/

# Build outputs
dist/
build/
*.pyc

# Environment
.env
.env.local

# IDE
.vscode/
.idea/
*.swp

# OS
.DS_Store
Thumbs.db

# Logs
*.log
```

### Removing Committed Secrets
```bash
# Rewrite history only with explicit approval and a rollback plan
git filter-repo --invert-paths --path <file>

# Or use BFG Repo-Cleaner when that tool is already approved and available
bfg --delete-files <file>
git reflog expire --expire=now --all
git gc --prune=now --aggressive
```

**Important**: Rotate compromised secrets immediately.

### Cleaning Up
```bash
git branch --merged         # List merged branches
git branch -d <branch>      # Delete merged branch
git remote prune origin     # Remove stale remote branches
git gc                      # Garbage collection
```

### Clean Push Hygiene
- Verify the diff matches the linked issue or named task before `git push`.
- Confirm generated files, lockfile churn, fixtures, and snapshots are intentional instead of accidental spillover.
- Reject pushes that include secrets, credentials, tokens, private keys, `.env` files, customer data, or other sensitive material.
- Keep CI or CD noise out of the branch unless the task explicitly asked for pipeline changes.

## Git Configuration

### User Setup
```bash
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
```

When a repository already has a local or global Git identity configured, preserve that identity for commits instead of inventing a separate assistant author label.

### Useful Aliases
```bash
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.lg "log --graph --oneline --decorate --all"
```

### Editor
```bash
git config --global core.editor "code --wait"  # VS Code
git config --global core.editor "vim"          # Vim
```

## Troubleshooting

### Detached HEAD
```bash
# Create branch from current state
git switch -c new-branch

# Or discard and return to branch
git switch main
```

### Merge vs Rebase
- **Merge**: Preserves history, creates merge commit
  - Use for: Integrating feature branches, shared branches
- **Rebase**: Linear history, no merge commits
  - Use for: Cleaning up local commits, updating feature branch
  - **Never rebase shared/public branches**

### Force Push Safety
```bash
# Safer than --force, fails if remote has new commits
git push --force-with-lease
```

### Large Files
Use Git LFS for large files:
```bash
git lfs install
git lfs track "*.psd"
git add .gitattributes
```

## Reference Files

Deep Git knowledge in references/:
- `00-git-knowledge-map.md` - Full capability matrix
- `10-safe-git-operations.md` - Safe operation guidelines
- `20-issue-branch-pr-flow.md` - Collaborative workflows
- `30-review-fix-and-human-handoff.md` - Review processes
- `40-recovery-and-incident-playbook.md` - Recovery procedures
- `99-source-anchors.md` - Authoritative sources

Load references as needed for specific topics.

## When to Use Multi-Agent

Use multi-agent only when the work clearly benefits from bounded parallel discovery or independent review, such as:
- Parallel branch ancestry or repository topology analysis across a large Git graph
- Independent comparison of conflict-resolution or history-rewrite options
- Read-only evidence collection from multiple remotes, worktrees, or issue trackers

OpenAI-aligned orchestration defaults:
- Use **agents as tools** when one manager should keep control of the user-facing turn, combine specialist outputs, or enforce shared guardrails and final formatting.
- Use **handoffs** when routing should transfer control so the selected specialist owns the rest of the turn directly.
- Use **code-orchestrated sequencing** for deterministic repository audits, explicit retries, or bounded parallel branches whose dependencies are already known.
- Hybrid patterns are acceptable when a triage agent hands off and the active specialist still calls narrower agents as tools.

Context-sharing defaults:
- Keep local runtime state and approvals separate from model-visible context unless they are intentionally exposed.
- Prefer filtered history or concise handoff packets over replaying the full transcript by default.
- Choose one conversation continuation strategy per thread unless there is an explicit reconciliation plan.
- Preserve workflow names, trace metadata, and validation evidence for multi-agent Git investigations.

Multi-agent discipline:
- Launch only non-overlapping workstreams and keep one active writer unless the user explicitly requests concurrent mutation.
- Wait on multiple agent IDs in one call instead of serial waits.
- Avoid tight polling; while agents run, do non-overlapping work such as synthesizing findings, reading adjacent history, or preparing validation.
- After integrating a finished agent's results, keep the agent available if that role is likely to receive follow-up in the current project; otherwise close it so it does not linger.
- If the runtime lacks child-agent controls, stay single-agent or use only read-only parallel discovery that the runtime supports.

Use single-agent for straightforward Git tasks or any task where a careful sequential audit is clearer.

### Required Lifecycle Rules

- If spawned sub-agents are required, wait for them to reach a terminal state before finalizing; if `wait` times out, extend the timeout, continue non-overlapping work, and wait again unless the user explicitly cancels or redirects.
- Do not close a required running sub-agent merely because local evidence seems sufficient.
- Keep at most one live same-role agent by default within the same project or workstream, maintain a lightweight spawned-agent list keyed by role or workstream, and check that list before every `spawn_agent` call. Never spawn a second same-role sub-agent if one already exists; always reuse it with `send_input` or `resume_agent`, and resume a closed same-role agent before considering any new spawn.
- Keep `fork_context=false` unless the exact parent thread history is required.
- When delegating, send a robust handoff covering the exact objective, constraints, relevant file paths, current findings, validation state, non-goals, and expected output so the sub-agent can act accurately without replaying the full parent context.

## Real-World Scenarios

- **Release Branch Rescue**: A release branch diverged under pressure and the team needs a safe merge, revert, or cherry-pick plan with rollback awareness.
- **History Repair Without Data Loss**: A branch contains bad commits, partial fixes, and shared history constraints; use this skill to separate reversible from destructive operations.
- **Tooling Mismatch**: A repo spans GitHub, GitLab, or local-only workflows; use this skill to adapt the plan to the tooling that is actually available instead of assuming one hosting CLI exists.

## Windows Environment

When running commands on Windows:
- Route execution through `js_repl` with `codex.tool(...)` first
- Inside `codex.tool("exec_command", ...)`, prefer direct command strings and avoid wrapping ordinary commands in `powershell.exe -NoProfile -Command "..."`
- Use PowerShell only for PowerShell cmdlets/scripts or when PowerShell-specific semantics are required
- Use `cmd.exe /c` for `.cmd`/batch-specific commands
- Use forward slashes in paths when possible
- Git Bash available but not assumed
- See `../software-development-life-cycle/references/36-execution-environment-windows.md` for details
- See `references/50-windows-git-workflows.md` for Windows-specific Git guidance

## Best Practices

1. **Commit Often**: Small, logical commits
2. **Meaningful Messages**: Explain what and why
3. **Pull Before Push**: Avoid conflicts
4. **Branch for Features**: Keep main stable
5. **Review Before Merge**: Code review catches issues
6. **Test Before Commit**: Don't break the build
7. **Keep History Clean**: Rebase local branches, squash when appropriate
8. **Prefer Worktrees For Parallel Features**: Keep issue lanes isolated instead of stacking unrelated changes
9. **Never Force Push Shared Branches**: Use `--force-with-lease` carefully
10. **Protect Secrets**: Never commit credentials
11. **Document Workflow**: Team conventions in README

## Safety Rules

### Never Do (Without Explicit User Request)
- Auto-commit changes
- Auto-push to remote
- Auto-merge branches
- Force push to shared branches
- Rewrite public history
- Delete branches without confirmation

### Always Do
- Explain what command will do
- Show current state before operations
- Warn about destructive operations
- Provide rollback instructions
- Verify user intent for risky operations

## Final Checklist

Before completing Git operations:
- [ ] Issue or task scope is identified and the branch stays feature-by-feature
- [ ] Worktree isolation is used when parallel issue lanes would otherwise collide
- [ ] Changes staged are correct and complete
- [ ] Commit message is clear and descriptive
- [ ] No secrets or sensitive data included
- [ ] Tests pass (if applicable)
- [ ] Branch is up to date with target
- [ ] Required CI/CD checks are green or the exception is explicitly approved
- [ ] User has confirmed destructive operations
- [ ] Rollback plan exists for risky operations

