Sync stale branches

From Bottelet/DaybydayCRM · View interactive version

Sync stale branches is an AI agent skill from the GitHub repository Bottelet/DaybydayCRM. Brings diverged remote branches up to date with develop — classifies, rescues unique work, then resets or deletes stale branches

How to use this skill

Skill file

Skill: sync-stale-branches

Bring old/diverged remote branches up to date with develop.

Run this periodically to keep the branch list clean and PR-able.


Inputs

Default: develop master


Step 1 — List candidate branches

git fetch --prune

# All remote branches minus the exclude list
git branch -r | grep -v 'origin/HEAD' \
  | sed 's|remotes/||' \
  | grep -v -E '^origin/(develop|master)$'

Add any other branches to exclude to the grep pattern.


Step 2 — Classify each branch

For every candidate origin/<branch>:

A — unique file count (three-dot diff from merge-base):

git diff --name-only origin/develop...origin/<branch> | wc -l

B — files ONLY in the branch (not in develop):

git diff --name-only --diff-filter=A origin/develop origin/<branch>

Classify as:


Step 3 — Handle EMPTY branches

These branches were never extended beyond the old fork point.

git push origin --delete <branch>

Step 4 — Handle COVERED branches

All unique files are already captured in a feature branch we are keeping.

Reset the branch to develop HEAD so it is current but carries no stale code.

git push origin origin/develop:refs/heads/<branch> --force

Step 5 — Handle HAS_UNIQUE branches

Rescue uncovered files before resetting.

5a — Identify which feature branch the files belong to

Group uncovered files by module/domain:

5b — Extract files onto the correct feature branch

On the target feature branch (must already exist and be ahead of develop):

git checkout origin/<stale-branch> -- <uncovered-file1> <uncovered-file2> ...
git add <files>
git commit -m "chore: rescue <description> from stale <stale-branch>"
git push origin HEAD --force-with-lease

If the target feature branch does not yet exist, use the feature-branch-extraction

procedure to create it properly on top of develop HEAD first.

5c — Reset the stale branch to develop

git push origin origin/develop:refs/heads/<branch> --force

Step 6 — Verify

# Confirm each branch is now equal to develop
for branch in <cleaned-branches>; do
  ahead=$(git rev-list origin/develop..origin/$branch --count)
  behind=$(git rev-list origin/$branch..origin/develop --count)
  echo "$branch → ahead=$ahead behind=$behind"
done

Expected: all cleaned branches show ahead=0 behind=0.


Notes

intend to update it.

Copilot will recreate them if needed.

Files the branch modifies relative to develop but which also exist in develop

are not "unique" — develop's version is preferred.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.