From a14f317d5adb45987445ae6a2c0b0c67d4cf3b47 Mon Sep 17 00:00:00 2001 From: shokollm <270575765+shokollm@users.noreply.github.com> Date: Fri, 27 Mar 2026 11:32:45 +0000 Subject: [PATCH] Add Branch Hygiene workflow section to SUBAGENT_WORKFLOW.md Document: - How to detect contamination via git log and branch --contains - Prevention with explicit base: git checkout -b new-branch main - Fix using git rebase --onto - Force push with --force-with-lease for safety Addresses Issue #1 comment 281 --- docs/SUBAGENT_WORKFLOW.md | 77 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/docs/SUBAGENT_WORKFLOW.md b/docs/SUBAGENT_WORKFLOW.md index 0f23dd3..e4cadf1 100644 --- a/docs/SUBAGENT_WORKFLOW.md +++ b/docs/SUBAGENT_WORKFLOW.md @@ -91,3 +91,80 @@ OPEN → IN_PROGRESS (subagent claims it) - Research/docs: `docs/issue-{N}-{short-title}` - Fixes/tools: `fix/issue-{N}-{short-title}` + +## Branch Hygiene + +When branches are created incorrectly (e.g., from HEAD instead of main), they become contaminated with unwanted commits. This section provides a standard workflow for detecting and fixing this. + +### How Contamination Happens + +- Running `git checkout -b new-branch` (without explicit base) creates a branch from the current HEAD +- If HEAD is not aligned with main (e.g., detached HEAD, or a different branch), the new branch inherits that history +- The branch then contains commits that don't belong to the intended base + +### Detection + +**Symptom:** `git log` shows commits from a different/wrong branch at the start of the history. + +**Command to identify contamination:** +```bash +# Find commits that exist in wrong-branch but not in main +git log main..wrong-branch --oneline + +# Check if a specific commit is contained in main +git branch --contains +# If empty output, the commit is NOT in main (contamination) + +# Or compare the first commit of your branch to main's tip +git merge-base main your-branch +# If this doesn't match the first commit on your branch, there's contamination +``` + +### Prevention + +**Always use explicit base when creating branches:** +```bash +# Correct - branch from main explicitly +git checkout -b new-branch main + +# Incorrect - branch from current HEAD (may not be main) +git checkout -b new-branch # DANGEROUS if HEAD isn't main +``` + +### Fix Procedure + +If contamination is detected, use `git rebase --onto` to move the branch to the correct base: + +```bash +# Syntax: git rebase --onto +git rebase --onto main wrong-branch new-branch + +# Example: +# - main is the correct base +# - wrong-branch is the contaminated branch (the old base that was used incorrectly) +# - new-branch is your current branch that has wrong commits + +# After rebase, verify with: +git log --oneline main.. +git branch --contains # Should be empty +``` + +### Force Push with Lease + +After rebasing, a force push is required. Use `--force-with-lease` for safety: + +```bash +git push --force-with-lease origin new-branch +``` + +`--force-with-lease` is safer than `--force` because it will fail if someone else has pushed to the branch since you last fetched, preventing accidental overwrites. + +### Quick Reference + +| Scenario | Command | +|----------|---------| +| Create clean branch | `git checkout -b new-branch main` | +| Detect contamination | `git log main..my-branch` (if non-empty, contaminated) | +| Check commit presence | `git branch --contains ` | +| Fix contaminated branch | `git rebase --onto main wrong-base my-branch` | +| Safe force push | `git push --force-with-lease origin my-branch` |