GeminiIntermediate

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.

8 minIntermediate

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.

zsh - reproduce
$npm test
FAIL src/cart.test.ts
Expected total 19.98 but received 1998
$

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.

gemini - debug
You
The cart total test fails: expected 19.98, got 1998. Find the cause but do not edit anything yet.
Agent
getTotal() sums prices stored in cents but the test expects dollars. The function returns cents without dividing by 100. I can fix it by returning total / 100. Shall I apply that?
Asking the agent to diagnose before editing.

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.

src/cart.ts
  function getTotal(items) {
-   return items.reduce((sum, i) => sum + i.priceCents, 0);
+   return items.reduce((sum, i) => sum + i.priceCents, 0) / 100;
  }
Approve narrowly
If the agent tries to touch unrelated files in the same edit, reject it and ask for a smaller change. Tight, single-purpose diffs are far easier to review and revert.

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.

zsh - verify
$npm test
PASS src/cart.test.ts
Tests: 1 passed, 1 total
$

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

Tags
#gemini-cli#debugging#coding#diff#workflow