How to Compare Two YAML Files Without Getting Bitten by Indentation
To compare two YAML files, bring them to the same indentation and key order, then put them side by side in a diff tool. That first part matters more with YAML than with almost any other format. A file re-indented from two spaces to four, or saved by an editor that alphabetizes keys, loads to exactly the same data and still shows up as a wall of red and green.
Below: why that happens, what each comparison method is good for, and a real Docker Compose change walked through from a noisy diff to the four lines that matter. The comparing itself happens on our YAML compare page, in your browser.
Why YAML files are deceptively hard to compare
YAML is a whitespace-sensitive format (see the YAML 1.2.2 specification), and that is exactly what makes diffs tricky. Indentation carries meaning, but the amount of indentation does not, as long as it is consistent. So one file indented with two spaces and another with four can load to the identical structure while every line looks different to a text diff.
Key order is the other half. A YAML mapping is a set of key/value pairs, and their order carries no meaning. These two container snippets are the same mapping:
image: nginx:1.27
imagePullPolicy: IfNotPresent
imagePullPolicy: IfNotPresent
image: nginx:1.27
A line diff still marks both lines as changed. Sequences are the exception: the items in a list keep their order, so moving a list item up or down is a real change.
| What you see in the diff | Is it a real change? | What to do |
|---|---|---|
| 2-space vs 4-space indent | No, only consistency matters | Re-emit both files with yq |
| Mapping keys in a different order | No, mappings are unordered | Sort keys on both sides with yq |
true vs "true" | Yes, boolean vs string | Investigate, this is real |
image: nginx vs image: "nginx" | No, same string value | Ignore, or normalize quoting |
Flow style [a, b] vs block list | No, same sequence | Pick one style for both |
| Sequence items in a different order | Yes, sequences are ordered | Investigate, this is real |
Quotes are where people get caught. true without quotes is a
boolean and "true" is a string, and mixing those up has
broken plenty of deployments. Quoting a plain word like nginx
changes nothing, though. The spec's section on
the core schema
lists exactly which unquoted values turn into booleans, numbers and nulls.
Ways to compare YAML
Which one you pick comes down to whether the files are on disk, in git, or in a chat message someone just sent you.
| Method | Good for | Handles key order? |
|---|---|---|
| Online side-by-side diff | Fragments pasted from a PR, a ticket or a chat | No, it compares the text as written |
yq then diff | Files on disk, CI scripts | Yes, once you sort the keys |
git diff | Changes already committed | No, line-based |
| dyff | Large Kubernetes manifests | Yes, it compares the parsed documents |
In practice the two mix well: let
yq
rewrite both files into one canonical layout, then read the result side by
side, where a changed value sits right next to the old one instead of
being split across a - and a + line.
Comparing two YAML files, step by step
This is what I run when two copies of a manifest disagree and nobody remembers who touched what.
- If the files came from different people or editors, normalize both:
yq -P 'sort_keys(..)' old.yaml > old.norm.yaml, and the same for the new file. That re-indents everything the same way and puts every mapping's keys in alphabetical order. - Open the YAML compare tool.
- Put the old file in the left pane and the new one in the right.
- Click Format on each side. It strips trailing spaces and extra blank lines at the end, which otherwise show up as changes you cannot see.
- Check the badge above each pane. A tab used for indentation is flagged with its line number, and it is the most common reason a YAML file refuses to load at all.
Step one does the heavy lifting. Our YAML tool compares the text exactly as you paste it and does not reorder keys for you, so a file sorted by yq and one that was not will still disagree on every line. The comparison itself uses Google's diff-match-patch and pairs each edited line with its original, so a changed value highlights only the characters that differ.
A worked example: a Docker Compose service
Here is a service definition from a Compose file, as it sits on the main branch:
services:
api:
image: shop/api:1.8.2
restart: unless-stopped
ports:
- "8080:8080"
environment:
LOG_LEVEL: info
CACHE_TTL: "300"
A teammate's branch has this. Their editor uses four spaces and sorted the keys on save:
services:
api:
environment:
CACHE_TTL: 300
LOG_LEVEL: debug
image: shop/api:1.9.0
ports:
- "8080:8080"
- "9090:9090"
restart: unless-stopped
Compared raw, every line except services: is marked as
changed. Run both through yq first and the diff shrinks to this:
| Path | Main | Branch | Change |
|---|---|---|---|
api.image | shop/api:1.8.2 | shop/api:1.9.0 | Modified |
api.environment.LOG_LEVEL | info | debug | Modified |
api.ports | 8080:8080 | 8080:8080, 9090:9090 | Item added |
api.environment.CACHE_TTL | "300" (string) | 300 (integer) | Type changed |
api.restart | unless-stopped | unless-stopped | No change (moved) |
Four edits. The image bump was the point of the branch. The debug log
level was probably left over from testing, and the extra 9090 port is the
sort of thing nobody mentions in the PR description. The quotes dropped
from CACHE_TTL are harmless in Compose, which passes every
environment value to the container as a string, but the same edit in a
config file your own service loads into a typed struct can become a
startup error.
The traps that are unique to YAML
A few YAML features cause diffs that surprise people. Anchors and aliases
(&name and *name) let one file repeat a value
by reference while another writes it out in full; both load to the same
data but read completely differently. The infamous "Norway problem" is
another: unquoted no, off, and yes
can parse as booleans in older YAML 1.1 parsers, so country: NO
might become false. YAML 1.2 fixed the schema, but plenty of
tools still ship 1.1 behavior. When a value looks like it changed type,
that is the first thing to check.
Everyday mistakes in YAML diffs
| Gotcha | Why it bites | Fix |
|---|---|---|
| Tabs for indentation | YAML forbids tabs for indentation; the file may not even parse | Convert tabs to spaces first |
| Quoted vs unquoted scalars | "true" is a string, true is a boolean | This can be a real change, do not dismiss it |
| Anchors and aliases | One file inlines a value, the other references it | Resolve anchors, then compare the expanded form |
| The Norway problem | no can parse as false in YAML 1.1 | Quote ambiguous strings; check your parser version |
| Trailing whitespace | Invisible spaces after a value show as a change | Trim trailing whitespace before comparing |
YAML and JSON are closer than they look
Every JSON document is valid YAML, because YAML 1.2 is a superset of JSON. That is handy for comparing: if the indentation or anchors are fighting you, convert both files to JSON, format them the same way, and diff that instead. Many parsers, including PyYAML, will round-trip YAML to a plain data structure you can re-emit as JSON, which strips away the stylistic differences and leaves only the data.
Related tools
YAML rarely travels alone. If you are comparing the JSON form of the same
data, JSON compare can format both sides
and sort the keys for you. Environment settings and .env
files line up well on the
config compare page, and reviewing
changes between two API calls is what
the API response diff is built for.
Release notes and changelogs that sit next to the manifests compare best
in the plain online text compare tool.
Frequently asked questions
- Is it safe to paste a Kubernetes manifest or CI config into an online YAML diff?
- On comparetext.org, yes. The YAML compare page diffs the two files with JavaScript in your browser tab, and nothing is stored or sent anywhere unless you press Share to create a link. Secrets are still secrets, though: if a manifest has real tokens or passwords inline, replace them with placeholders before you paste it into any web page, including ours.
- Why does my YAML diff show every line as changed?
- Usually one file was re-indented (two spaces versus four) or saved by an editor that sorts keys, so every line moved even though the data is the same. Run both files through yq -P with sort_keys(..) to give them identical indentation and key order, then compare again. What is left is the set of values that really changed, often just a few lines.
- Does indentation width matter when comparing YAML?
- Not for meaning, only for consistency. YAML uses indentation to show structure, but it does not care whether you use two spaces or four, as long as each level is consistent within the file. So a two-space file and a four-space file can hold identical data while looking entirely different to a text diff. Re-emitting both with the same tool removes that false difference. Tabs, however, are not allowed for indentation at all.
- How do I compare YAML while ignoring key order?
- Sort the keys before you compare. YAML mappings are unordered, so two files with the same keys in a different order hold the same data, but a text diff cannot know that. The comparetext.org YAML page compares text as written and does not reorder keys, so run yq -P with sort_keys(..) on both files first, or use a YAML-aware tool such as dyff. Leave list items in their original order, because sequence order is meaningful.
- What is the Norway problem in YAML?
- It is a classic YAML gotcha where the unquoted value NO is parsed as the boolean false instead of the string "NO", so a country code for Norway becomes false. It happens in YAML 1.1 parsers, which treat yes, no, on, and off as booleans. YAML 1.2 narrowed the rules, but many tools still ship 1.1 behavior. If a value looks like it flipped from a word to true or false in your diff, an unquoted boolean-like string is the likely cause. Quoting the value fixes it.
- How big a YAML file can I compare in the browser?
- A few thousand lines is fine; a long Helm chart output or a big OpenAPI spec compares as you type. The page lines files up line by line before looking at individual characters, which keeps it quick. Once you are into multi-megabyte dumps of a whole cluster, a terminal tool like dyff or git diff is the better choice, since it does not have to render both files on screen.
Got two versions of a config? Normalize them with yq, then paste both into the YAML compare tool.