Git history cleanup

From tksuoran/erhe · View interactive version

Git history cleanup is an AI agent skill from the GitHub repository tksuoran/erhe. Collapse many WIP / fix-to-fix commits on a feature branch into a small set of topic-focused commits. Reads the original commit messages first so intent is preserved, soft-resets to origin/main so intermediate / overwritten changes drop out automatically, then re-commits the accumulated diff one log

How to use this skill

Skill file

Git history cleanup

Collapse origin/main..HEAD into a small number of topic-focused commits without losing intent. Changes that were introduced in one commit and rewritten or undone by a later commit drop out automatically -- they are not in the final diff. The original commit messages are read first so the new topic split preserves the why of the work.

The previous tip of the branch is kept by git reflog (default 90 days), so the operation is non-destructive locally. Nothing is pushed.

Step 0 -- working copy must be clean

git status --porcelain

If the output is non-empty, STOP. Tell the user to commit, stash, or discard their pending edits first. A dirty tree would mix uncommitted work into the collapse and there is no clean way to un-mix afterwards. Do not improvise an auto-stash; the user may not want those edits in the regrouped history.

Step 1 -- agent context must be clean

This task reads every commit message between origin/main and HEAD, plus the full accumulated diff, plus the per-file change set. That is heavy and benefits from a fresh context window so nothing important gets truncated mid-analysis.

Self-assess:

When in doubt, prefer spawning the agent; the overhead is small and the failure mode of running out of context mid-analysis is bad (a half-grouped history with no record of the original intent).

Step 2 -- study the existing commits

Read full bodies, not just subjects:

git log origin/main..HEAD --pretty=format:'%n=== %h %an %ad%n%s%n%n%b'
git log origin/main..HEAD --name-status

For each commit capture:

Save the output, or at least your notes from it, somewhere reachable in step 4 -- once the reset happens these messages are still in the reflog but reading them again costs another pass.

Also useful when the branch is long or branchy:

git log origin/main..HEAD --merges            # are there merge commits in the range?
git log origin/main..HEAD --reverse --oneline # chronological order, easier for narrative

If there are merge commits, surface them to the user and ask whether to flatten -- this skill assumes a linear range.

Step 3 -- collapse to origin/main

The user's phrasing "soft reset" here means "non-destructive reset that preserves all changes". Use --mixed (the git default) so everything lands as unstaged working-tree changes -- that is what step 4 analyzes and step 5 selectively stages per topic:

git reset --mixed origin/main

Equivalent two-step that honors the literal "soft" wording:

git reset --soft origin/main
git reset HEAD --

After either form:

Do NOT use git reset --hard here. --hard discards the working tree, which erases the entire point of the operation.

Step 4 -- group the diff by topic

git status
git diff --stat
git diff

Cluster the changes into logical topics:

Present the proposed split to the user before committing anything:

Wait for the user to confirm or redirect. Do NOT silently regroup.

Step 5 -- commit each topic

Before the first commit, snapshot the pre-commit diff so step 6 can verify nothing was dropped (<scratch> = the session's scratch directory, outside the repo):

git diff > <scratch>/git-history-cleanup-target.diff

Then, for each topic in order:

  1. Stage exactly that topic:

```sh

git add path/to/whole_topic_files

git apply --cached <hunk.patch> # only when a file has mixed-topic hunks

```

git add -p is interactive and cannot be driven from this tool: for a mixed file, write per-hunk patches to the scratch directory and stage them with git apply --cached. Do not paper over difficulty by lumping a mixed file into one topic if it really belongs to two -- that defeats the purpose of the skill.

  1. Sanity check before committing:

```sh

git diff --cached # should show ONLY this topic

git diff # should show everything still pending for later topics

```

  1. Commit using the project's style (match git log):
  1. Move on to the next topic.

Step 6 -- verify and stop

After the last topic commit, BOTH of these must hold:

git status                          # must be clean -- no remaining changes
git diff origin/main..HEAD > <scratch>/git-history-cleanup-after.diff
diff <scratch>/git-history-cleanup-target.diff <scratch>/git-history-cleanup-after.diff

The diff must be empty (or only differ in trailing whitespace). Equivalently, the total diff vs. origin/main must equal the diff captured before any topic commits.

If either check fails a hunk was dropped or duplicated. DO NOT rescue with a catch-all "misc" / "wip" commit -- that defeats the purpose. Find the missing hunk, decide which topic it belongs to, and either amend that topic's commit (only if it is the most recent one) or do a fresh git reset --mixed origin/main and re-run from step 5.

Show the user the new shortlog and stop:

git log origin/main..HEAD --oneline

Pushing is the user's call.

Hard rules

Recovery

If the user dislikes the new history, the previous tip is in the reflog:

git reflog | head -30
git reset --hard <old-tip-sha>

When reporting completion to the user, mention the old tip SHA explicitly (capture it from git rev-parse HEAD BEFORE the step-3 reset) so they know the safety net is one command away.

When NOT to use

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.