| name | git-operations |
| description | Automate git commands. Use for commits, branches, merges, or repository management tasks. |
Git Operations
Automate git workflows using subprocess or GitPython.
Quick Start
import subprocess
def run_git(args: list[str]) -> str:
result = subprocess.run(
["git"] + args,
capture_output=True,
text=True,
check=True
)
return result.stdout.strip()
# Get current branch
branch = run_git(["branch", "--show-current"])
# Check status
status = run_git(["status", "--porcelain"])
Common Patterns
Stage and commit
run_git(["add", "."])
run_git(["commit", "-m", "Your commit message"])
Branch operations
# Create and switch to branch
run_git(["checkout", "-b", "feature/new-feature"])
# List branches
branches = run_git(["branch", "-a"]).splitlines()
# Delete branch
run_git(["branch", "-d", "old-branch"])
Check for changes
def has_changes() -> bool:
status = run_git(["status", "--porcelain"])
return bool(status)
Get commit info
# Latest commit hash
commit_hash = run_git(["rev-parse", "HEAD"])
# Commit message
message = run_git(["log", "-1", "--format=%s"])
# Files changed in last commit
files = run_git(["diff", "--name-only", "HEAD~1"]).splitlines()
Safe operations
def safe_checkout(branch: str) -> bool:
try:
run_git(["checkout", branch])
return True
except subprocess.CalledProcessError:
return False