How to Fix a Bug in Your Codebase with the Gemini CLI
Use the Gemini CLI agent to locate, diagnose, and patch a real bug, then review the diff before accepting.
The Gemini CLI is an agent, not just a chat box. It can read files, run your tests, propose an edit, and apply it once you approve. This guide walks through using it to chase down a failing test and ship a fix you actually understand.
What you need
- The Gemini CLI installed and signed in
- A project with a reproducible bug or a failing test
- Git initialized so you can review and revert changes
- About 10 minutes
Step 1: Reproduce the failure first
Before involving the agent, run the failing test yourself so you have a clear signal. Knowing the exact error message lets you verify the fix later instead of trusting the agent blindly.
Step 2: Describe the bug to the agent
Launch gemini in the project folder and describe the symptom precisely, pasting the failing assertion. Ask it to investigate before changing anything, which keeps the first response focused on diagnosis.
Step 3: Review the proposed diff
When the agent proposes an edit, the CLI shows a colored diff and asks you to approve, reject, or modify. Read it carefully. Accept only if the change matches your understanding of the bug from step 1.
function getTotal(items) {
- return items.reduce((sum, i) => sum + i.priceCents, 0);
+ return items.reduce((sum, i) => sum + i.priceCents, 0) / 100;
}Step 4: Confirm the fix
After the edit is applied, ask the agent to run the tests, or run them yourself. Seeing the same test pass confirms the fix without you having to take the agent's word for it.
Result
The failing test now passes from a one-line change you read and understood. Because git tracked the edit, you can review git diff one more time and commit, or revert in a second if you change your mind.
Watch related tutorials
19:27
16:12
25:00
14:00
15:00
1:42:18