Review an agent's diff in the terminal with git diff A terminal-focused guide by Flavio shows how to review a coding agent's changes with git diff options, using a real throwaway Express shop repository where the agent on branch agent/discount-cap changed 15 files, including 151 insertions and 147 deletions across 13 tracked files. The guide recommends starting with git status -sb to catch untracked files that git diff omits, then using git add -N on the three new files (src/discount.js, src/format.js, test/discount.test.js) so they appear in the diff and the src/utils.js to src/format.js rename is detected. It warns that git add -N . also stages deletions, which made the rename disappear from git diff. Review an agent's diff in the terminal with git diff By Flavio https://flaviocopes.com/about/ The git diff options that make a coding agent's change quick to review: stats, pathspecs, whitespace, moved code, renames, three dots, add -p and delta. A coding agent finishes a task and tells you it changed 15 files. Before you accept anything, you want to know what it really did. Plain git diff dumps everything at once, in file order, with the lockfile right in the middle. Git has plenty of options to cut that down, and most people never use them. In this post, we’ll review one agent change from start to finish, right in the terminal. I ran every command below on a real throwaway repository, so you can see what each option does to the same diff. The example change The project is a tiny Express shop with a cart, a discount function, a few routes and tests. The agent worked on the agent/discount-cap branch, where we asked it to stop the FLAT5 discount code from making a cart total negative. It did that, and also: - moved applyDiscount from src/cart.js to a new src/discount.js - renamed src/utils.js to src/format.js - reindented src/api.js and the routes from 4 spaces to 2 - bumped Express, which rewrote package-lock.json - touched a few other things it wasn’t asked to touch That mix is typical: a few lines of real fixes buried inside moves, renames and formatting. Our goal is to dig out the lines that change behavior. If you run agents in parallel, each one usually gets its own branch in a Git worktree https://flaviocopes.com/git-worktrees/ . Everything we’ll run here works the same inside a worktree. Here’s the order we’ll follow: Start with git status -sb Let’s start with git status -sb . It gives us the short format plus the branch line, one line per file: git status -sb agent/discount-cap M README.md M docs/CHANGELOG.md M package-lock.json M package.json M src/api.js M src/cart.js M src/config.js M src/index.js M src/routes/checkout.js M src/routes/products.js M src/stats.js D src/utils.js M test/cart.test.js ?? src/discount.js ?? src/format.js ?? test/discount.test.js Notice the three ?? lines, which are new files the agent created without adding them. git diff doesn’t show untracked files at all , so if you skip git status , you’ll miss them completely: git diff --stat README.md | 2 +- docs/CHANGELOG.md | 5 ++ package-lock.json | 200 +++++++++++++++++++++++++---------------------- package.json | 4 +- src/api.js | 24 +++--- src/cart.js | 15 +--- src/config.js | 2 +- src/index.js | 3 +- src/routes/checkout.js | 6 +- src/routes/products.js | 9 ++- src/stats.js | 8 +- src/utils.js | 14 ---- test/cart.test.js | 6 +- 13 files changed, 151 insertions + , 147 deletions - src/discount.js is missing, and src/utils.js looks deleted. The fix is git add -N intent to add . It records those paths in the index without their content, so git diff starts showing them as new files: git add -N src/discount.js src/format.js test/discount.test.js Now git status also detects the rename: agent/discount-cap M README.md M docs/CHANGELOG.md M package-lock.json M package.json M src/api.js M src/cart.js M src/config.js A src/discount.js R src/utils.js - src/format.js M src/index.js M src/routes/checkout.js M src/routes/products.js M src/stats.js M test/cart.test.js A test/discount.test.js Be careful with git add -N . , because a . pathspec also stages deletions. That moved src/utils.js into the index as deleted and made the rename disappear from git diff , so name the new files instead. Get the shape with —stat and friends --stat gives us the table of contents of a diff. With the new files included, we get: git diff --stat README.md | 2 +- docs/CHANGELOG.md | 5 ++ package-lock.json | 200 ++++++++++++++++++++++++-------------------- package.json | 4 +- src/api.js | 24 +++--- src/cart.js | 15 +--- src/config.js | 2 +- src/discount.js | 9 ++ src/{utils.js = format.js} | 2 +- src/index.js | 3 +- src/routes/checkout.js | 6 +- src/routes/products.js | 9 +- src/stats.js | 8 +- test/cart.test.js | 6 +- test/discount.test.js | 11 +++ 15 files changed, 172 insertions + , 134 deletions - --compact-summary prints the same table with new and gone markers, making created files easy to spot: git diff --compact-summary README.md | 2 +- docs/CHANGELOG.md | 5 ++ package-lock.json | 200 ++++++++++++++++++++++++-------------------- package.json | 4 +- src/api.js | 24 +++--- src/cart.js | 15 +--- src/config.js | 2 +- src/discount.js new | 9 ++ src/{utils.js = format.js} | 2 +- src/index.js | 3 +- src/routes/checkout.js | 6 +- src/routes/products.js | 9 +- src/stats.js | 8 +- test/cart.test.js | 6 +- test/discount.test.js new | 11 +++ 15 files changed, 172 insertions + , 134 deletions - When we only want the totals, --shortstat keeps it to one line: git diff --shortstat 15 files changed, 172 insertions + , 134 deletions - --name-status prints one letter per file, plus a similarity score for renames. It’s the easiest format to paste into a script or back into the agent’s chat: git diff --name-status M README.md M docs/CHANGELOG.md M package-lock.json M package.json M src/api.js M src/cart.js M src/config.js A src/discount.js R086 src/utils.js src/format.js M src/index.js M src/routes/checkout.js M src/routes/products.js M src/stats.js M test/cart.test.js A test/discount.test.js R086 means Git considers src/format.js 86% similar to src/utils.js . Use --name-only when you only want the file paths. --dirstat shows where the change is concentrated, directory by directory: git diff --dirstat 5.2% src/routes/ 15.5% src/ 5.1% test/ By default, --dirstat counts changed lines and skips the repository root. Because the 200-line lockfile and the README don’t get a row, the percentages don’t add up to 100. With --dirstat=files,cumulative , we count files instead and roll subdirectories into their parent: git diff --dirstat=files,cumulative 6.6% docs/ 13.3% src/routes/ 60.0% src/ 13.3% test/ Cut the noise with pathspecs Everything after -- is a pathspec , a filter that controls which files the diff covers. A pathspec starting with : excludes files. Here the lockfile is 200 of the 306 changed lines, so let’s drop it first: git diff --stat -- . ': package-lock.json' README.md | 2 +- docs/CHANGELOG.md | 5 +++++ package.json | 4 ++-- src/api.js | 24 ++++++++++++------------ src/cart.js | 15 +++------------ src/config.js | 2 +- src/discount.js | 9 +++++++++ src/{utils.js = format.js} | 2 +- src/index.js | 3 ++- src/routes/checkout.js | 6 +++--- src/routes/products.js | 9 +++++---- src/stats.js | 8 ++++---- test/cart.test.js | 6 +++--- test/discount.test.js | 11 +++++++++++ 14 files changed, 62 insertions + , 44 deletions - Quote the pathspec, or your shell will try to expand the . If you prefer the explicit form, ': exclude package-lock.json' does the same thing. You can stack several exclusions at once: git diff --stat -- . ': package-lock.json' ': .md' A glob limits the diff to one kind of file: git diff --stat -- ' .js' src/api.js | 24 ++++++++++++------------ src/cart.js | 15 +++------------ src/config.js | 2 +- src/discount.js | 9 +++++++++ src/{utils.js = format.js} | 2 +- src/index.js | 3 ++- src/routes/checkout.js | 6 +++--- src/routes/products.js | 9 +++++---- src/stats.js | 8 ++++---- test/cart.test.js | 6 +++--- test/discount.test.js | 11 +++++++++++ 11 files changed, 54 insertions + , 41 deletions - And a directory path limits it to that folder: git diff --stat -- src/routes src/routes/checkout.js | 6 +++--- src/routes/products.js | 9 +++++---- 2 files changed, 8 insertions + , 7 deletions - The package.json diff is only 4 lines, so we can read that directly to see which versions changed. The lockfile follows from it. Hide whitespace with -w src/api.js has 24 changed lines. Here’s the start of it: git diff src/api.js js - api.post '/cart/summary', req, res = { - const { items, code } = req.body - if Array.isArray items { - return res.status 400 .json { error: 'items must be an array' } - } - res.json cartSummary items, code - } + api.post '/cart/summary', req, res = { + const { items, code } = req.body + if Array.isArray items { + return res.status 422 .json { error: 'items must be an array' } + } + res.json cartSummary items, code + } It looks like a reindent from 4 spaces to 2. Is there anything else hiding in there? Let’s add -w --ignore-all-space to ignore whitespace when comparing lines: git diff -w src/api.js diff --git a/src/api.js b/src/api.js index 07f05b9..7031e39 100644 --- a/src/api.js +++ b/src/api.js @@ -7,7 +7,7 @@ export function createApi { api.post '/cart/summary', req, res = { const { items, code } = req.body if Array.isArray items { - return res.status 400 .json { error: 'items must be an array' } + return res.status 422 .json { error: 'items must be an array' } } res.json cartSummary items, code } The agent changed the status code from 400 to 422 right inside the reindent. That’s an API change clients might depend on, buried in 24 lines of formatting. -w also works with --stat , so we can see which files are formatting only: git diff -w --stat -- src src/api.js | 2 +- src/cart.js | 15 +++------------ src/config.js | 2 +- src/discount.js | 9 +++++++++ src/{utils.js = format.js} | 2 +- src/index.js | 1 + src/routes/checkout.js | 2 +- src/routes/products.js | 1 + src/stats.js | 8 ++++---- 9 files changed, 22 insertions + , 20 deletions - src/routes/products.js dropped from 9 lines to 1, and that single line is an extra blank line: js @@ -4,6 +4,7 @@ const products = { id: 3, name: 'Backpack', price: 4900 } + export function productList req, res { res.json products } --ignore-blank-lines hides hunks made only of blank lines. Combined with -w , the file disappears from the stat: git diff -w --ignore-blank-lines --stat -- src/routes src/routes/checkout.js | 2 +- 1 file changed, 1 insertion + , 1 deletion - It only hides a hunk when every changed line in it is blank. A blank line next to a real change stays visible. Treat -w as a reading aid. Don’t stage or commit from a -w view without checking the full diff too, because whitespace matters in Python, YAML and Makefiles. Find small edits with —word-diff and —color-words A one-word change inside a long line is easy to miss because Git prints the whole line twice. --word-diff shows the changed words inline, using -removed- and {+added+} markers: git diff --word-diff README.md Discount codes are -checked- {+validated+} on the server, and the cart total is always calculated in cents to avoid rounding errors. It works on code too. Here the agent changed the tax rate, which is easy to miss in a line-based diff: git diff --word-diff src/config.js js @@ -1,3 +1,3 @@ export const TAX RATE = -0.22- {+0.21+} export const PORT = 3000 export const CURRENCY = 'EUR' We never asked for a tax rate change, so it goes on our list of things to reject. --color-words drops the bracket markers and highlights changes in red and green it’s a shorthand for --word-diff=color : Git splits words on whitespace by default, so 0.22 and 0.21 show up as whole words. If you need finer splits, --word-diff-regex lets you define what counts as a word. And if you’re parsing output in a script, --word-diff=porcelain prints one token per line. Follow moved code with —color-moved The agent moved applyDiscount into its own file and changed one line along the way. In a normal diff, that looks like a wall of red in src/cart.js and a wall of green in src/discount.js , forcing you to compare them line by line. --color-moved paints lines removed in one place and added in another with separate colors. With dimmed-zebra , untouched moved lines fade to gray so the real edits jump out: git diff --color-moved=dimmed-zebra src/cart.js src/discount.js The gray lines moved as is. Look at what stayed in full color: - return total - 500 became return Math.max 0, total - 500 , which is the fix we asked for. - shipping discounted became shipping sub , which nobody asked for. Free shipping is now based on the subtotal before the discount. You’ll notice the last few lines of the function also kept their color. Move detection only triggers on blocks of at least 20 alphanumeric characters, and the } and return total lines after the edit are too short on their own. Without a mode, --color-moved defaults to zebra , which alternates colors on moved blocks instead of dimming them. Setting diff.colorMoved in ~/.gitconfig turns it on for every diff. If a moved block was also reindented say, a function moved inside an object , move detection misses it. Pass --color-moved-ws=allow-indentation-change to ignore indentation shifts when every line in the block moved by the same amount: git diff --color-moved=dimmed-zebra --color-moved-ws=allow-indentation-change Rename detection with -M Git doesn’t store renames. Instead, it detects them by pairing a deleted file with an added file that shares similar content. git diff enables this by default, so we already saw src/{utils.js = format.js} in the stat. -M --find-renames is the explicit flag, and --no-renames turns it off: git diff --no-renames --stat -- src/utils.js src/format.js src/format.js | 14 ++++++++++++++ src/utils.js | 14 -------------- 2 files changed, 14 insertions + , 14 deletions - With rename detection on, those 28 lines shrink to 2: git diff -- src/utils.js src/format.js diff --git a/src/utils.js b/src/format.js similarity index 86% rename from src/utils.js rename to src/format.js index ce4d3c1..f20b06a 100644 --- a/src/utils.js +++ b/src/format.js @@ -1,5 +1,5 @@ export function formatPrice cents, currency = 'EUR' { - return new Intl.NumberFormat 'en-US', { + return new Intl.NumberFormat 'it-IT', { style: 'currency', currency } .format cents / 100 The agent changed the price locale while renaming the file. That’s another unrequested edit, and it’s easy to miss when a rename appears as a 14-line deletion and a 14-line addition. Notice that I passed both paths. If you put only src/format.js in the pathspec, Git can’t see the deleted file to pair them up, so it prints a brand new file. -M also accepts a similarity threshold 50% by default . With -M100% , Git only accepts exact renames, so this one falls apart: git diff -M100% --stat -- src/utils.js src/format.js src/format.js | 14 ++++++++++++++ src/utils.js | 14 -------------- 2 files changed, 14 insertions + , 14 deletions - Better hunks with —diff-algorithm=histogram In src/stats.js , the agent added a weight function at the top and deleted the unused revenue function. Here’s what Git’s default algorithm Myers produces: git diff src/stats.js @@ -1,15 +1,15 @@ -export function count items { +export function weight items { let sum = 0 for const item of items { - sum += item.quantity + sum += item.grams item.quantity } return sum } -export function revenue items { +export function count items { let sum = 0 for const item of items { - sum += item.price item.quantity + sum += item.quantity } return sum } That’s the smallest possible diff, and it’s misleading to read. Because the functions share most of their lines, it looks like count was rewritten into weight and revenue into count . --diff-algorithm=histogram anchors on lines that appear less often, like function names: git diff --diff-algorithm=histogram src/stats.js js @@ -1,3 +1,11 @@ +export function weight items { + let sum = 0 + for const item of items { + sum += item.grams item.quantity + } + return sum +} + export function count items { let sum = 0 for const item of items { @@ -5,11 +13,3 @@ export function count items { } return sum } - -export function revenue items { - let sum = 0 - for const item of items { - sum += item.price item.quantity - } - return sum -} Now the diff reflects what happened: one function added, one removed, and count untouched. It has 16 lines instead of 8, and it’s much easier to read. --diff-algorithm=patience is the algorithm histogram extends, and on this file it gave the same output as Myers. I’d set histogram as my default, and remember --histogram and --patience work as short flags. More or less context with -U and —function-context By default, Git shows 3 lines of context around each change, and -U