diff --git a/skills/git-workspace/SKILL.md b/skills/git-workspace/SKILL.md new file mode 100644 index 0000000..5d2b889 --- /dev/null +++ b/skills/git-workspace/SKILL.md @@ -0,0 +1,50 @@ +--- +name: git-workspace +description: >- + Manage Git branches, worktrees, history integration (rebase, squash, cherry-pick), + conflict stops, and evidence-first reflog recovery. Use when creating branches or + worktrees, synchronizing branches, editing history, or recovering lost commits. + Not for slicing checkpoint commits: use git-commit. +--- + +# Git Workspace + +## Use when +- Creating dedicated feature branches or isolated worktrees before making edits. +- Integrating branches (rebase, squash, merge, cherry-pick) or resolving conflicts. +- Recovering lost commits, dangling refs, or broken working-tree state via reflog. +- Not for slicing uncommitted diffs into checkpoint commits: use `git-commit`. + +## Steps +1. **Inspect state.** Resolve target repository root (`git rev-parse --show-toplevel`), active branch, + upstream tracking, clean vs. dirty status (`git status --short`), and reflog head. All commands execute + strictly from the target repository root. +2. **Branch and worktree management.** Enforce the branch-before-edits invariant. Create dedicated feature + branches (`git switch -c `). When working across parallel tasks or spikes without disturbing current + working-tree state, create an isolated worktree following [references/worktrees.md](references/worktrees.md). + Never remove a worktree containing uncommitted or unmerged work without explicit confirmation. +3. **Integration and history editing.** Rebase local unshared branches onto upstream base; collapse fixup + checkpoints via squash; cherry-pick with dependency checks. On any merge or rebase conflict: stop immediately, + report conflicting files and hunks in prose, never guess resolution, and offer clean abort (`git rebase --abort`). + Follow [references/history-editing.md](references/history-editing.md). +4. **Evidence-first recovery.** When a commit, branch, or working-tree state appears lost or corrupted, inspect + evidence first (`git reflog`, `git fsck`, `git branch -v`) to identify exact candidate SHAs and timestamps + before proposing any fix. Never run destructive resets (`git reset --hard`) as a speculative remedy. Restore + discovered state into a dedicated recovery branch. Follow [references/recovery.md](references/recovery.md). +5. **Verify post-operation state.** Confirm target ref, commit log (`git log -n 3 --oneline`), and clean working + tree before concluding. + +## Your call +- **Branch and worktree naming and target paths**: choose names and locations for new branches or worktrees. +- **Integration strategy**: choose rebase vs. merge when an architectural choice exists. +- **Conflict resolution**: adjudicate conflicting hunks when git stops during integration. +- **Consequential and destructive actions**: require explicit confirmation with impact described first per + `profile/AGENTS.md` before running destructive commands (`git reset --hard`, force-push, worktree removal). + +## Done when +- Target branch or worktree is created, integrated, or safely cleaned up; or lost ref is restored to a named + branch with observable verification evidence. + +## Hands off to +- `git-commit`: when code changes are ready to be sliced, reviewed, and committed. +- `code-review`: when a branch or PR diff is ready for multi-axis static review before integration. diff --git a/skills/git-workspace/references/history-editing.md b/skills/git-workspace/references/history-editing.md index 9015c22..13b31d4 100644 --- a/skills/git-workspace/references/history-editing.md +++ b/skills/git-workspace/references/history-editing.md @@ -9,16 +9,18 @@ Rules for rewriting or combining history. Read before any of these operations. ## Rebase - Fine on local, unshared branches, to keep history linear and tidy before integrating. - Never rebase or force-push a branch others may have pulled without my explicit OK. +- On conflicts: stop immediately. Inspect `git status --short` and conflicting hunks. Never guess resolution. Offer `git rebase --abort` to return cleanly to prior state if resolution is uncertain. ## Cherry-pick 1. Inspect the source commit and what it depends on. 2. Confirm the target working tree is clean. 3. Cherry-pick, inspect the resulting diff, run validation. -4. On conflicts or hidden dependencies: stop and report. +4. On conflicts or hidden dependencies: stop and report. Offer `git cherry-pick --abort`. ## Merge - Follow the repo's integration policy (merge commit vs fast-forward vs rebase). - Keep merge commits when the branch shape tells a useful story (long-running feature work). +- On conflicts: stop and report. Offer `git merge --abort` before attempting resolution. ## Reset and restore - **Safe** (keeps file contents): `git restore --staged `, `git reset --soft `. diff --git a/skills/git-workspace/references/recovery.md b/skills/git-workspace/references/recovery.md new file mode 100644 index 0000000..53f6873 --- /dev/null +++ b/skills/git-workspace/references/recovery.md @@ -0,0 +1,76 @@ +# Evidence-First Git Recovery + +Reference for `git-workspace`. Procedures for diagnosing and recovering lost commits, detached heads, botched rebases, or corrupted refs using observable evidence. + +## Core Invariant +Never propose a destructive command (`git reset --hard`, `git checkout -f`, `git clean -fd`) as a speculative remedy. Destructive commands discard uncommitted files permanently. Always inspect evidence first, locate candidate commit SHAs, and restore state into a new named branch. + +## 1. Inspect Current State Before Acting + +Check working-tree and ref state: +```sh +git status --short +git branch -v +git log -n 5 --oneline +``` +If working-tree changes exist, stash or commit them before attempting ref recovery: +```sh +git stash push -m "WIP before reflog recovery" +``` + +## 2. Reflog Inspection + +The reflog records every update to local branch tips and `HEAD`. + +- Inspect recent `HEAD` movements: + ```sh + git reflog -n 20 --date=iso + ``` +- Look for state transitions: + - `rebase (finish)` / `rebase (start)`: find the commit immediately preceding `rebase (start)`. + - `reset: moving to ...`: find the SHA prior to the reset. + - `commit: ...`: locate orphaned commits from deleted or moved branches. + +- Inspect branch-specific reflog: + ```sh + git reflog show + ``` + +## 3. Verify Candidate Commit + +Inspect the candidate SHA before restoring: +```sh +git show --stat +git log -1 -p +``` +Confirm that the commit contains the expected changes and history. + +## 4. Non-Destructive Restoration + +Never mutate current branch in place with `--hard` reset. Instead, point a dedicated recovery branch at the discovered SHA: +```sh +git branch recovery/ +``` +Verify the recovered branch: +```sh +git log -n 3 --oneline recovery/ +``` +Then switch to the recovery branch cleanly: +```sh +git switch recovery/ +``` + +## 5. Dangling and Unreachable Objects (Last Resort) + +If a commit was not captured in the reflog (e.g. reflog expired or repository corruption): +```sh +git fsck --lost-found +``` +Dangling commits appear in `.git/lost-found/commit/`. Inspect each candidate: +```sh +git log -1 -p +``` +Once found, create a branch off it: +```sh +git branch recovery/ +``` diff --git a/skills/git-workspace/references/worktrees.md b/skills/git-workspace/references/worktrees.md new file mode 100644 index 0000000..039a13f --- /dev/null +++ b/skills/git-workspace/references/worktrees.md @@ -0,0 +1,52 @@ +# Git Worktrees + +Reference for `git-workspace`. Manage isolated working trees linked to the same underlying Git repository without cloning or stashing. + +## Use Cases +- Working on a quick fix or spike without stashing or switching branches in your main working tree. +- Running parallel test suites or builds in separate checkouts. +- Evaluating a PR branch while keeping current development intact. + +## Path Conventions +- Place worktrees outside the primary repository directory (for example, `../-` or in a dedicated sibling directory), or under an ignored `.worktrees/` directory. +- Never nest a worktree inside a tracked source directory. + +## Core Operations + +### 1. Add Worktree +- For an existing branch: + ```sh + git worktree add ../- + ``` +- For a new branch off current base: + ```sh + git worktree add -b feat/ ../- + ``` + +### 2. Inspect Worktrees +- List all active worktree paths and their checked-out branches/SHAs: + ```sh + git worktree list + ``` + +### 3. Pinning / Locking +- Prevent accidental pruning when a worktree sits on removable or shared storage: + ```sh + git worktree lock --reason "active long-running test build" + git worktree unlock + ``` + +### 4. Safe Removal +- Confirm working-tree status inside the worktree first: + ```sh + git -C status --short + ``` +- If clean, remove cleanly: + ```sh + git worktree remove + ``` +- If dirty, **never run `git worktree remove --force`** without explicit confirmation from the human owner describing what uncommitted edits will be discarded. +- If a worktree directory was deleted manually, clean up administrative metadata: + ```sh + git worktree prune + ```