From cogni-ai-git-ops
Usage for basic Git operations and repository management. This is the core skill for all git interactions. You MUST load this skill when using git command-line.
How this skill is triggered — by the user, by Claude, or both
Slash command
/cogni-ai-git-ops:gitThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
<!-- markdownlint-disable MD013 -->
Expert in advanced git usage for repository agents. Prioritize non-interactive, safe, reproducible operations that maintain clean history and respect repository conventions.
gh instead).git commit --amend, or git reset --hard) as a blind fallback when a prior
command fails.
If an operation fails unexpectedly, you MUST immediately halt state-mutation and execute purely
read-only diagnostic commands (git status, git log) to re-sync your mental model.<type>[optional scope]: <description> with a blank line and detailed
body if needed.--no-verify only as a last resort when hooks are
non-critical or environment-specific.-i, --interactive,
default editors).git add, git commit, git rm), always verify your environmental context (e.g., checking
git status or pwd to ensure you aren't inadvertently crossing submodule boundaries).
Observe and Orient using read-only commands to establish ground-truth before you Decide and Act.git commit --amend --no-edit --date="$(git log -1 --format=%aD)"git mv old_file new_file (instead of mv or rm)git commit --fixup <commit-sha>-i):
git rebase -i --autosquash origin/maingit fetch origin && git rebase origin/main --no-verify (add --no-verify only if
hooks block)git cherry-pick -x <commit-sha> (-x records original SHA for traceability)GIT_EDITOR=true git cherry-pick --continue (auto-accept commit message during
conflict resolution)git stash push -m "wip-description" -- path/to/file (use pathspec for
selective stashing; avoid -p as it is interactive)Git commands that normally open an editor can be made non-interactive:
GIT_EDITOR=true git <command> (auto-accepts default message)git commit --amend --no-edit, git merge --no-edit, git revert --no-editgit commit -m "message" (avoids editor entirely)GIT_EDITOR=true git cherry-pick --continue (after resolving conflicts)git revert <commit-sha> --no-edit (uses default revert message)Important: Automation tools (like report_progress) may fail if they encounter editor prompts.
Always use non-interactive patterns when preparing changes for automated tools.
GitHub Actions and other CI environments often check out repositories as shallow clones (limited history).
Note: This repository is configured to check out as unshallow by default (fetch-depth: 0) in its main workflows.
test -f .git/shallow && echo "Shallow clone" || echo "Full clone"git rev-parse --is-shallow-repository (outputs true or false)git fetch --unshallow origin <current-branch>
(retrieves complete history for the current branch).
Then fetch target branches if needed (e.g., git fetch origin <target-branch>).
Avoid using just git fetch --unshallow as it may fail or be incomplete in CI environments.git cat-file -e <commit-sha> 2>/dev/null && echo "Exists" || echo "Not found"git log --all --oneline | grep <commit-sha-prefix>git fetch origin pull/<pr-number>/head:pr-<pr-number>git branch -a --contains <commit-sha>git diff --quiet && git diff --cached --quietgit rev-parse --abbrev-ref HEADgit log origin/$(git rev-parse --abbrev-ref HEAD)..HEADgit reflog before history-rewriting operations to enable rollback.git tag backup/pre-op-$(date +%s)git push --force-with-lease origin <branch>git commit, verify that there are no unresolved merge conflict markers. Use git diff --cached to ensure no markers (e.g., <<<<<<<, =======, >>>>>>>) are being committed. NEVER commit a file containing unresolved merge conflicts.git revert <commit-sha> --no-edit.<<<<<<<, =======, and >>>>>>> markers are removed.GIT_EDITOR=true git revert --continue.git config commit.templategitGraph commit lines:
git):
git log origin/main..HEAD --reverse --format='commit id: "%s"'
(Note: if commit subjects contain " characters, escape them so Mermaid string parsing stays valid—see the
GitGraph guidance in mermaid/SKILL.md.)gh api):
gh api repos/<owner>/<repo>/pulls/<number>/commits --jq '.[] | "commit id: \"[\(.sha[0:7])] \(.commit.message | split("\n")[0] | gsub("\""; "'\''"))\""'gh):
gh pr view <number> --json headRefName,baseRefName,commitsgit --no-pager log --oneline --decorate --graph --all --max-count=20 --simplify-by-decorationgit status -sbgit log --show-signature -1git config user.name && git config user.emailgit log --oneline origin/main..origin/feature/branch (alternative to gh pr view)When a merge introduces too many unrelated changes, maintain PR focus with selective conflict resolution:
git reset --hard <commit-sha> (reset to commit before problematic merge)git revert -m 1 <merge-commit-sha> --no-edit (revert merge keeping first parent)rm .git/index.lock (if git operations fail due to lock)git ls-tree -r <commit-sha>:.github/workflows/ --name-only (list files at specific
commit)git diff <commit-sha> HEAD --name-status (compare commits to see what changed)Strategy for focused PRs:
git reset --hard to target commit with desired changes onlyProblem: When asked to "integrate changes from dev/main", using git merge <target-branch> creates a merge commit
that includes ALL commits from the target branch in your PR, making it impossible to review only your feature changes.
Solution: Rebase your commits on top of the target branch using the cherry-pick workflow.
Identify your feature commits:
# List commits in your branch not in target
git log --oneline origin/<target-branch>..HEAD
# Save commit SHAs for later
git log --oneline origin/<target-branch>..HEAD | awk '{print $1}' | tac
Fetch latest target branch:
git fetch origin <target-branch>
Create backup before destructive operation:
git tag backup/before-rebase-$(date +%s)
Reset branch to target (creates clean starting point):
git reset --hard origin/<target-branch>
Cherry-pick your feature commits:
# Method 1: Individual commits
git cherry-pick <commit-1> <commit-2> <commit-3>
# Method 2: Range syntax (note the ^)
git cherry-pick <first-commit>^..<last-commit>
Handle conflicts during cherry-pick:
git add <resolved-files>GIT_EDITOR=true git cherry-pick --continuegit cherry-pick --skipgit cherry-pick --abortVerify only your changes are present:
# Check changed files
git diff origin/<target-branch>..HEAD --name-status
# Check stats
git diff origin/<target-branch>..HEAD --stat
# Verify commit count matches expectations
git log --oneline origin/<target-branch>..HEAD | wc -l
The diff should show ONLY files you created/modified for your feature, not unrelated files from target branch.
<<<<<<<, =======, and >>>>>>>.CRITICAL: Automation tools like report_progress automatically attempt to rebase your branch against the
remote tracking branch. This causes session crashes when:
The crash sequence:
git rebase origin/<your-branch> automatically# After reset + cherry-pick workflow is complete
git checkout -b <feature>-v2
# or
git checkout -b <feature>-rebased
# Now report_progress won't attempt rebase - no remote tracking branch exists yet
If you must reuse the same branch:
# Complete all operations manually
git fetch origin <target-branch>
git reset --hard origin/<target-branch>
git cherry-pick <commits...>
# Verify changes
git diff origin/<target-branch>..HEAD --stat
# DON'T call report_progress here - ask user to push instead
# Explain: "Branch ready, needs force-push: git push --force-with-lease origin <branch>"
# If you haven't pushed anything yet
git checkout -b <new-feature-branch> origin/<target-branch>
# Do your work
# Call report_progress - safe because no divergence
Error patterns indicating auto-rebase crash:
Rebasing (1/NNN) where NNN is very large (e.g., 113 commits)error: could not apply <commit-sha>...CONFLICT (content): Merge conflict in <file>warning: skipped previously applied commitGitError: rebase git errorIf automation tool fails with rebase errors:
git rebase --abortgit log --oneline -5 and git diff origin/<target-branch>..HEAD --statgit checkout -b <branch>-v2 && git push origin <branch>-v2<sha>, needs manual push: git push --force-with-lease origin <branch>"Best Practice: Complete all git operations manually (fetch, reset, cherry-pick, verify) BEFORE calling automation tools. The tool will then simply push your clean, prepared commits.
# 1. Identify your commits
git log --oneline origin/dev..HEAD
# Output: abc123 Feature commit 3
# def456 Feature commit 2
# ghi789 Feature commit 1
# 2. Fetch latest dev
git fetch origin dev
# 3. Backup and reset
git tag backup/before-dev-$(date +%s)
git reset --hard origin/dev
# 4. Cherry-pick in original order
git cherry-pick ghi789 def456 abc123
# 5. Verify only your 3 commits on top of dev
git log --oneline origin/dev..HEAD
git diff origin/dev..HEAD --stat
# 6. Branch is now clean and ready for automation tools or manual push
git push fails with rejected ... (fetch first)
because the remote contains new work you do not have locally, NEVER forcefully overwrite it.
In non-GitHub-Actions environments, pull the latest changes first
(e.g. git pull --rebase origin <branch>) to apply your local commits on top of the remote changes,
resolve conflicts if any, and then retry git push.
In GitHub Actions runtime, use git pull --no-rebase origin <branch> (merge semantics) instead,
as the workflow's auto-PR/push logic requires compatible remote branch history.git rebase -i, git add -p without scripting, editor prompts).git pull in scripts - prefer explicit fetch + rebase or merge --no-edit.--force pushes without --force-with-lease.git reset --hard (prefer git reset --hard origin/main with backup tag).git rm instead of rm.Always explain proposed git operations step-by-step, quote exact commands, and confirm irreversible actions with the user.
To fully utilize this skill, you MUST read at least one of the links relevant to the current context:
git filter-branch for history rewriting and subdirectory extraction tasks.git bisect to track down regressions or bugs.git reflog to recover lost commits, branches, or undo destructive operations.Guides creation, editing, and verification of skills for AI coding agents using test-driven development with subagent scenarios. Use when authoring or debugging skills.
npx claudepluginhub cogni-ai-ou/cogni-ai-agentic-collections --plugin cogni-ai-git-ops