home / skills / laurigates / claude-plugins / git-cli-agentic

git-cli-agentic skill

/git-plugin/skills/git-cli-agentic

This skill optimizes git workflows for AI agents by delivering porcelain output and deterministic patterns across status, diff, log, and branch commands.

npx playbooks add skill laurigates/claude-plugins --skill git-cli-agentic

Review the files below or copy the command above to add this skill to your agents.

Files (1)
skill.md
6.1 KB
---
model: haiku
name: git-cli-agentic
description: Git commands optimized for AI agent workflows with porcelain output and deterministic execution patterns.
allowed-tools: Bash(git status *), Bash(git diff *), Bash(git log *), Bash(git branch *), Bash(git remote *), Bash(git add *), Bash(git commit *), Bash(git push *), Bash(git restore *), Read
created: 2025-01-16
modified: 2026-01-31
reviewed: 2025-01-16
---

# Git CLI Agentic Patterns

Optimized git commands for AI agent consumption using porcelain output and stable formats.

## Core Principle

Use `--porcelain` for machine-readable output that remains stable across Git versions and user configurations.

## Working Directory

**Run git commands directly — your working directory is the repo:**
```bash
git status
git log --oneline -5
git diff --stat
```

The `-C` flag is only needed when targeting a different repository from your current directory:
```bash
# Submodule: run command against parent repo
git -C "$(git rev-parse --show-toplevel)" remote get-url origin

# Script: iterate over multiple repos
for repo in repos/*; do
  git -C "$repo" status --porcelain
done
```

## Status Operations

### Porcelain Status

```bash
# Version 2 porcelain with branch info (recommended)
git status --porcelain=v2 --branch

# Version 1 porcelain (simpler)
git status --porcelain

# Short format (human-readable but stable)
git status --short --branch
```

**Porcelain v2 Format**:
```
# branch.oid <commit>
# branch.head <branch>
# branch.upstream <upstream>
# branch.ab +<ahead> -<behind>
1 <XY> <sub> <mH> <mI> <mW> <hH> <hI> <path>
2 <XY> <sub> <mH> <mI> <mW> <hH> <hI> <X><score> <path><tab><origPath>
? <path>
! <path>
```

**Status Codes**:

| Code | Meaning |
|------|---------|
| M | Modified |
| A | Added |
| D | Deleted |
| R | Renamed |
| C | Copied |
| ? | Untracked |
| ! | Ignored |

### Quick Checks

```bash
# Check if clean (empty output = clean)
git status --porcelain

# Count changed files
git status --porcelain | wc -l

# Check for uncommitted changes
git diff --quiet || echo "has changes"
```

## Diff Operations

### Stat Output

```bash
# File change summary
git diff --stat

# Numeric stats (machine-readable)
git diff --numstat

# Name and status only
git diff --name-status

# Names only
git diff --name-only
```

**Numstat Format**: `<added>\t<deleted>\t<filename>`

### Staged vs Unstaged

```bash
# Unstaged changes
git diff --numstat

# Staged changes
git diff --cached --numstat

# Both (working tree vs HEAD)
git diff HEAD --numstat
```

### Specific Comparisons

```bash
# Against specific commit
git diff $COMMIT --numstat

# Between branches
git diff main..feature --numstat

# Between commits
git diff $COMMIT1..$COMMIT2 --name-status
```

## Log Operations

### Custom Format

```bash
# Hash and subject only
git log --format='%H %s' -n 10

# Oneline (built-in)
git log --oneline -n 10

# With stats
git log --oneline --stat -n 5

# Machine-parseable with multiple fields
git log --format='%H|%an|%ae|%s' -n 10
```

**Format Placeholders**:

| Placeholder | Meaning |
|-------------|---------|
| `%H` | Full commit hash |
| `%h` | Short hash |
| `%s` | Subject |
| `%b` | Body |
| `%an` | Author name |
| `%ae` | Author email |
| `%ad` | Author date |
| `%cn` | Committer name |

### Filtering

```bash
# By author
git log --author="name" --oneline -n 10

# By date range
git log --since="2025-01-01" --oneline

# By path
git log --oneline -n 10 -- path/to/file

# Merge commits only
git log --merges --oneline -n 5
```

## Branch Operations

### Branch Info

```bash
# List with tracking info
git branch -vv

# Formatted output
git branch --format='%(refname:short) %(upstream:short) %(upstream:track)'

# Current branch only
git branch --show-current

# Remote branches
git branch -r --format='%(refname:short)'
```

### Tracking Status

```bash
# Ahead/behind count
git rev-list --left-right --count origin/main...HEAD

# Output: <behind>\t<ahead>
```

## Remote Operations

```bash
# List remotes with URLs
git remote -v

# Get specific remote URL
git remote get-url origin

# Show remote details
git remote show origin
```

## Staging Operations

```bash
# Stage specific files
git add path/to/file

# Stage all modified tracked files
git add -u

# Stage everything
git add -A

# Unstage file
git restore --staged path/to/file

# Discard changes
git restore path/to/file
```

## Commit Operations

```bash
# Simple commit
git commit -m "message"

# With body (heredoc)
git commit -m "$(cat <<'EOF'
Subject line

Body paragraph.

Co-Authored-By: Name <email>
EOF
)"

# Amend last commit (use carefully)
git commit --amend -m "new message"
```

## Push Operations

```bash
# Push current branch
git push origin HEAD

# Push to different remote branch (main-branch development)
git push origin main:feature-branch

# Push commit range
git push origin start^..end:feature-branch

# Set upstream
git push -u origin HEAD
```

## Agentic Optimizations

| Context | Command |
|---------|---------|
| Quick status | `git status --porcelain=v2 --branch` |
| Changed files | `git diff --name-status` |
| Staged changes | `git diff --cached --numstat` |
| Recent commits | `git log --format='%h %s' -n 5` |
| Branch tracking | `git branch -vv --format='%(refname:short) %(upstream:track)'` |
| Current branch | `git branch --show-current` |

## Error Handling in Context

Use `2>/dev/null` to suppress errors in context expressions (do NOT use `||` fallbacks - blocked by Claude Code 2.1.7+):

```markdown
- Git status: !`git status --porcelain=v2 --branch 2>/dev/null`
- Current branch: !`git branch --show-current 2>/dev/null`
- Remote URL: !`git remote get-url origin 2>/dev/null`
```

## Combining with GH CLI

For GitHub-specific operations, combine with `gh` commands:

```bash
# Get repo owner/name
gh repo view --json nameWithOwner --jq '.nameWithOwner'

# Then use in git operations
git push origin main:$(gh pr view --json headRefName --jq '.headRefName')
```

## Best Practices

1. **Use porcelain v2** for status when parsing programmatically
2. **Use --numstat** for diff when counting changes
3. **Use custom --format** for log when extracting specific fields
4. **Always add 2>/dev/null fallback** in context expressions
5. **Prefer git switch/restore** over checkout for clarity

Overview

This skill provides a curated set of Git CLI patterns optimized for AI agent workflows. It focuses on porcelain output, deterministic formats, and stable command patterns to make parsing and automation reliable. The commands emphasize machine-readable outputs, predictable diff/log formats, and safe workspace checks.

How this skill works

The skill maps common repository inspection and manipulation tasks to specific git commands that return stable, machine-parseable output (porcelain, numstat, formatted logs). It prefers porcelain v2 for status, --numstat for diffs, and custom --format for logs. Commands are presented with examples for working-directory contexts, staged vs unstaged checks, branch and remote queries, and push/stage/commit flows.

When to use it

  • When building or running autonomous agents that must parse Git output reliably
  • When scripting multi-repo operations and you need deterministic results
  • When extracting change metrics (added/deleted lines, file lists) for analysis
  • When you need concise, machine-parseable commit history or branch tracking info
  • When integrating git checks into CI, bots, or deployment automation

Best practices

  • Use git status --porcelain=v2 --branch for programmatic workspace state
  • Use git diff --numstat or --name-status for stable diff summaries
  • Prefer custom git log --format='%H|%an|%ae|%s' for extracting exact fields
  • Run git commands in the repo working directory; use -C only when targeting a different repo
  • Suppress incidental stderr in context expressions with 2>/dev/null (avoid || fallbacks)

Example use cases

  • Agent checks workspace cleanliness: git status --porcelain and git diff --quiet to gate actions
  • Collect changed-file counts for reporting: git status --porcelain | wc -l or git diff --numstat
  • Produce machine-readable commit lists for release notes: git log --format='%H|%s' -n 50
  • Automated branch sync checks: git rev-list --left-right --count origin/main...HEAD to get ahead/behind counts
  • Staging and committing in scripts: git add -u; git commit -m 'msg' and use git push origin HEAD for deterministic pushes

FAQ

Why use porcelain output?

Porcelain formats are stable across Git versions and user configs, making them suitable for automated parsing and agent consumption.

When should I use --numstat vs --name-status?

Use --numstat when you need added/deleted line counts per file. Use --name-status when you only need file names plus change type (A/M/D).

How do I check if the working tree is clean in scripts?

Run git status --porcelain and check for empty output, or use git diff --quiet || echo 'has changes' to detect modifications.