AI Coding Tip 001 - Commit Before Prompt
Source: Dev.to

A safety‑first workflow for AI‑assisted coding
TL;DR
Commit your code before asking an AI Assistant to change it.
Common Mistake ❌
Developers ask an AI assistant to “refactor this function” or “add error handling” while they have uncommitted changes from their previous work session. When the AI makes its changes, the git diff shows everything mixed together—their manual edits plus the AI’s modifications—making it hard to separate what broke and to revert safely.
Problems Addressed 😔
- Mixing previous code changes with AI‑generated code.
- Losing track of what was changed.
- Struggling to revert broken suggestions.
How to Do It 🛠️
- Finish your manual task.
- Run your tests to ensure everything passes.
- Commit your work with a clear message, e.g.
feat: manual implementation of X. - (No need to push the commit.)
- Send your prompt to the AI assistant.
- Review the changes using your IDE’s diff tool.
- Accept or revert: keep the changes if they look good, or run
git reset --hard HEADto instantly revert. - Run the tests again to verify the AI changes didn’t break anything.
- Commit AI changes separately, e.g.
refactor: AI‑assisted improvement of X.
Benefits 🎯
- Clear diffing – see the AI’s suggestions in isolation.
- Easy revert – undo a bad AI hallucination instantly.
- Context control – ensure the AI works on your latest, stable logic.
- Tests stay green – you’re not breaking existing functionality.
Context 🧠
When you ask an AI to change your code, it might delete crucial logic or rename variables across several files. Uncommitted changes hide what the AI did versus what you did manually. By committing first you create a safety net; git diff shows exactly what the AI modified, and a single git reset --hard HEAD restores the clean state. Working in very small increments further reduces risk. Some assistants are not good at undoing their changes.
Prompt Reference 📝
git status # Check for uncommitted changes
git add . # Stage all changes
git commit -m "msg" # Commit with message
git diff # See AI's changes
git reset --hard HEAD # Revert AI changes
git log --oneline # View commit history
Considerations ⚠️
Only necessary if you work in write mode and your assistant is allowed to modify the code.
Type 📝
- Semi‑Automatic – you can enforce rules for the assistant to check repository status before making changes.
Limitations ⚠️
If your code is not under a source‑control system, you’ll need to apply this workflow manually.
Tags 🏷️
- Complexity
Level 🔋
- Beginner
Related Tips 🔗
- Use TCR
- Use Vibe Test Driven Development
- Break large refactorings into smaller prompts
- Git Bisect for AI Changes: using
git bisectto identify which AI‑assisted commit introduced a defect - Reverting Hallucinations
Conclusion 🏁
Treating AI as a pair programmer requires the same safety practices you’d use with a human collaborator: version control, code review, and testing. Committing before prompting creates clear checkpoints that make AI‑assisted development safer and more productive. This simple habit turns AI from a risky black box into a powerful tool you can experiment with confidently, knowing you can always return to a working state. Commit early, commit often, and don’t let AI touch uncommitted code.
More Information ℹ️
Tools 🧰
GIT is an industry standard, but you can apply this technique to any other version‑control software.
This article is part of the AI Coding Tip series.