Articles
Published · November 18, 2026

YAML Anchors, Aliases, and Merge Keys: Reuse Without Copying

An anchor (&) marks a YAML node, an alias (*) repeats it, and the << merge key folds one mapping into another. Here is how each works, who wins on conflict, and where it breaks.

Also in: Español中文हिन्दी

Quick answer

In YAML, &name marks a node, *name repeats it later, and <<: *name merges a mapping into the one you are writing. Together they let you define a block of config once and reuse it, instead of pasting copies that slowly drift apart.

Anchors and aliases are part of the YAML spec itself. The << merge key is a separate, older convention that most parsers support but you should still check.

How it actually works

Anchors and aliases: mark once, repeat later

The & indicator puts an anchor on a node. The * indicator is an alias that points back to it. The YAML 1.2.2 spec puts it this way: the first occurrence of a node is identified by an anchor, and each subsequent occurrence is an alias node that refers back to that anchor.

default_timeout: &timeout 30
build:
  timeout: *timeout
test:
  timeout: *timeout

Both timeout keys resolve to 30. Edit the first line and both follow. An anchor works on any node: a scalar, a list, or a whole mapping.

A few spec rules are worth knowing:

  • An alias must refer to a previous node. The spec says each alias "shall refer to a previous node identified by the anchor", and a processor should reject unidentified aliases. Define the anchor first.
  • An anchor does not have to be used. An unreferenced &name is legal.
  • Anchor names do not have to be unique. An alias refers to the most recent node with that anchor name.

The << merge key: reuse and override

Aliasing a whole mapping gives you an exact copy. Often you want a copy with changes. That's what the merge key is for.

defaults: &defaults
  adapter: postgres
  host: localhost
  pool: 5

development:
  <<: *defaults
  database: app_dev

production:
  <<: *defaults
  host: db.example.com
  database: app_prod

development ends up with adapter, host, pool, and database. production does too, but its own host wins. The merge key type definition says each key/value pair of the merged mapping is inserted into the current mapping, "unless the key already exists in it". Keys you write yourself always beat merged ones.

You can merge several mappings by giving << a list:

base: &base { retries: 3, verbose: false }
fast: &fast { retries: 1 }

job:
  <<: [*fast, *base]
  name: deploy

The type definition says keys in mappings earlier in the sequence override keys in later ones. Here job gets retries: 1 from *fast, verbose: false from *base, and its own name. The same page gives an example with << : [ *BIG, *LEFT, *SMALL ] followed by an explicit x: 1, where the explicit key wins over the whole list.

Not in the YAML 1.2.2 text

Here's the catch. The merge key is documented in a YAML 1.1 working draft dated 2005. The 1.2.2 spec text we read describes anchors and aliases but does not describe <<. Many parsers still implement it, though some need it switched on. Treat it as a convention to test, not a guarantee.

When to use it (and when to skip it)

Use anchors when the same block really is the same thing: shared environment variables, default settings, or a common set of steps. A good test is whether you would want a change to land in every copy at once.

GitHub Actions is a concrete example. GitHub's changelog announced YAML anchor support on September 18, 2025, enabled for all Actions users and repositories. The docs show an env: &env_vars block on one job reused as env: *env_vars on another, and a whole job anchored as &base_job and reused as an alias. If you are new to the file layout, see a GitHub Actions workflow file first. The docs we read do not mention merge keys, so check your platform before using << in a workflow.

Skip anchors when the likeness is coincidence. Two blocks that look alike today but belong to different services should stay separate. Otherwise a "small tweak" to the shared block breaks both.

Before you reuse anything, check these pitfalls:

  • Order matters. An alias before its anchor is an error, so keep anchors near the top.
  • The spec only promises pair insertion. The merge definition covers inserting keys unless they exist. It does not say how nested mappings combine, so do not assume a deep merge. Test your parser.
  • Quoting still applies. An aliased scalar is the same value everywhere, including surprises like YAML booleans.
  • Long values. Anchoring a block scalar works, and YAML multiline strings explains the indicators you will use inside one.

How Carets fits in

Anchors and merge keys live in config and CI files, which are plain text you often tweak on the go. Carets is a native notes and code editor for iPhone, iPad, and Mac, with syntax highlighting for code and config files, so &, *, and << stand out instead of blending into the indentation. That makes a missing anchor or a misplaced alias easier to spot.

Your files stay plain text, organized into files, projects, and tags, so a config snippet library sits next to your notes. If you also keep TOML around, TOML tables are a good companion read. For YAML work on iPhone, iPad, or Mac, Carets is the editor we built for it.

Download Carets on the App Store for iPhone, iPad, and Mac.

Frequently Asked Questions

Is the YAML merge key part of the YAML 1.2 spec?

The << key is defined in a 2005 working draft for YAML 1.1. The 1.2.2 spec text we read does not describe it, so support depends on your parser. Test it before you rely on it.

Can an alias come before its anchor?

No. The 1.2.2 spec says each alias shall refer to a previous node identified by the anchor, and a processor should reject unidentified aliases.

Do local keys or merged keys win?

Local keys. A merge inserts a pair unless the key already exists in the current mapping. In a list of mappings, earlier ones override later ones.

Do anchors work across files?

An alias refers to a node earlier in the same document. We didn't find any source describing cross-file anchors, so for sharing across files you need a tool-specific mechanism.