Refactoring an unfamiliar codebase with an LLM usually feels like juggling live grenades. One wrong hallucination in an abstract class, and three dependent modules break silently. Most AI coding assistants hide behind ephemeral buffer edits or internal rollback caches. Aider does not. It anchors every single code transformation directly into standard Git commits.
At 365Finds, we consider Aider's decision to treat Git as the single source of truth one of the cleanest architectural choices in developer tooling. It turns chaotic AI refactoring into an auditable, deterministic sequence of micro-checkpoints. But that tight coupling also means Git becomes both your safety net and your hazard zone if you do not understand how Aider commits, inspects, and rolls back code.
How Aider Tracks Changes: The Auto-Commit Loop
Every time Aider accepts a user prompt and successfully writes modified code to your repository, it executes a two-stage Git sequence:
- Pre-edit Dirty State Preservation: If you have uncommitted manual edits in your working directory when issuing a prompt, Aider automatically commits those existing changes first. This isolates your human edits from the upcoming AI modifications.
- Atomic AI Commit: Once Aider modifies the target files, it stages them and creates an atomic Git commit accompanied by a descriptive, LLM-generated commit message (e.g.,
refactor: extract token validator into separate helper).
Because each turn maps 1:1 to a real Git commit hash, tracking changes during an intensive refactoring session does not require special proprietary tooling. You track Aider exactly as you track a human pair programmer:
In-Session Tracking Commands
/diff: Displays the unified Git diff generated by the most recent prompt. Run this immediately after Aider finishes responding to verify that the refactoring did not touch unintended lines./git <command>: Runs native Git commands without leaving the chat session. Typing/git log -n 5 --onelinereveals the exact lineage of micro-commits Aider has generated during the active session./git status: Confirms whether the working tree is clean or if untracked side-effect files exist.
# Example flow inside Aider
aider > /git log -n 3 --oneline
a1b2c3d refactor: extract database connection logic into DBConnection class
e4f5a6b aider: commit dirty files before AI edits
789def0 initial project setup
How Aider Reverts Commits: The Mechanics of /undo
When an automated refactor goes sideways—say, Aider renames a public interface but forgets to update callers across legacy packages—you do not need to manually run git reset in a split terminal. You type /undo.
aider > /undo
What /undo Actually Executes
At 365Finds, we look closely at what happens behind the interface. /undo is not merely a wrapper around git reset --hard HEAD~1; it performs two simultaneous operations:
- Working Tree and Commit Rollback: It executes a hard Git reset against the commit preceding Aider's last auto-commit, completely restoring the working tree and index to their previous states.
- Contextual Memory Eviction: Critically, Aider evicts the discarded prompt, the AI's response, and the broken code changes from the active LLM conversational context window.
This second step is where Aider outperforms standard editor undo buttons. When you manually undo an AI edit in an editor buffer, the LLM often still remembers the failed refactor in its session memory. Subsequent prompts suffer from context poisoning, where the model continues referencing hallucinations from its previous response. Aider's /undo synchronizes the LLM's memory with the Git tree, ensuring the model's next attempt starts from clean ground.
Stepping Back Multiple Iterations
/undo is stack-based. If a refactoring sequence went down the wrong path across three prompts, running /undo three consecutive times rolls back three auto-commits and clears three conversational turns sequentially.
Targeted Rollbacks and Edge Cases
While /undo handles the immediate previous step, long refactoring sessions present scenarios where a blind hard reset is dangerous:
| Refactoring Scenario | Recommended Action | Underlying Command / Mechanism |
|---|---|---|
| Immediate bad edit on the last turn | /undo in chat | Hard Git reset + conversational context eviction |
| Reverting an older turn without losing subsequent good refactors | Selective Git revert | /git revert <commit-hash> |
| Inspecting uncommitted manual tweaks | Pre-commit diff review | /diff |
| Preserving failed code as a manual scratchpad | Manual soft reset outside Aider | git reset --soft HEAD~1 in a separate terminal |
| Skipping noisy CI hooks during rapid AI iterations | Default Aider setting (--no-verify) | Pass --git-commit-verify only if strict linters must block bad AI commits |
Danger Zone: Manual Edits Mid-Session
If you manually edit a file in your IDE while an Aider session is running, and then immediately run /undo before giving Aider another prompt, standard git reset --hard behavior will wipe out your uncommitted manual edits.
If you make manual changes mid-session, either run /commit to checkpoint them intentionally, or inspect your working tree with /git status before executing /undo.
The 365Finds Recommended Refactoring Workflow
At 365Finds, our commercial testing reveals that Aider's auto-commit mechanism is exceptionally reliable, but it produces severe Git log pollution if used directly on trunk branches. A 30-minute refactoring dialogue can easily introduce 25 micro-commits with messages of varying quality.
To maintain clean repository hygiene while leveraging Aider's auto-commits and /undo safety net, we recommend the following four-step protocol:
- Isolate on a Dedicated Refactor Branch:
Never launch Aider refactoring sessions on
mainordevelop. Create an isolated branch:git checkout -b refactor/modernize-auth-service - Leave Auto-Commits Active:
Do not start Aider with
--no-auto-commits. Disabling auto-commits disables/undo's underlying mechanism and forces you to manually stage and commit every exploratory edit, defeating Aider's velocity advantage. - Automate Failure Detection:
Run Aider with your test runner configured using
--test-cmd:
If Aider's refactored code breaks unit tests, it automatically captures the test output and attempts self-repair before committing. If self-repair fails, executeaider --test-cmd "npm test"/undo. - Squash-Merge into Production:
Once the refactoring is complete and all integration tests pass, close the Aider session. Do not push the raw commit history to remote. Squash the entire refactor branch into a single, cohesive, reviewable commit:
git checkout main git merge --squash refactor/modernize-auth-service git commit -m "refactor(auth): modernize token verification and extract session store"
Aider solves the scariest part of AI refactoring—uncontrolled code drift—by delegating control to Git. Use its auto-commits to experiment without fear, use /undo to scrub failed reasoning from both disk and memory, and clean the commit log at the branch boundary.