Mermaid Git Graph Syntax & Examples
Reference for Mermaid gitGraph syntax — commit, branch, checkout, merge, and tag operations. Copy-paste examples for gitflow, trunk-based, and release branching strategies.
Use this page to connect a high-intent search query to the right problem-solution narrative.
A syntax reference for Mermaid's `gitGraph` grammar — read this when you need the exact way to declare a commit, switch branches, merge with a tag, or pick a different layout orientation.
View Mermaid sourcePlain-text diagram syntax — copy or edit directly.
1gitGraph2 commit id: "init"3 branch develop4 checkout develop5 commit id: "feature A"6 branch feature/login7 checkout feature/login8 commit id: "login scaffold"9 commit id: "login tests"10 checkout develop11 merge feature/login12 checkout main13 merge develop tag: "v1.0.0"What is a Mermaid git graph
A git graph is a visualisation of a git branching workflow — commits, branches, merges, and tags rendered as nodes and lines. It is the right shape when the question is what-does-our-branching-strategy-look-like: onboarding documentation explaining gitflow, contributing guides showing the expected branch model, release-process documentation showing how main / develop / feature branches interact. A small git graph at the top of a CONTRIBUTING.md replaces multiple paragraphs of prose with one scannable visual. The diagram doesn't show real commits from a repository — it's an illustrative model showing what the workflow looks like, not what actually happened.
Basic syntax — gitGraph + commit + branch
Every git graph starts with `gitGraph`. The most basic operation is `commit` — adds a commit to the current branch. `commit id: "label"` gives the commit an explicit name; without it Mermaid generates a hash-like id automatically. `branch <name>` creates a new branch and switches to it. `checkout <name>` switches to an existing branch. `merge <name>` merges the named branch into the current one. These four primitives — commit, branch, checkout, merge — cover the vast majority of real git graphs. Commits accumulate on whatever branch is currently checked out, just like in real git.
- `gitGraph` — diagram type keyword (always first)
- `commit` — adds a commit to the current branch
- `branch <name>` — create new branch and switch to it
- `checkout <name>` — switch to existing branch
- `merge <name>` — merge named branch into current
Commit labels & types
The `commit` operation accepts several modifiers. `commit id: "name"` sets an explicit commit id (recommended for readability). `commit tag: "v1.0.0"` adds a release tag. `commit type: HIGHLIGHT` flags a notable commit with distinct styling. `commit type: REVERSE` shows a revert. The three commit types — `NORMAL` (default), `REVERSE`, `HIGHLIGHT` — are useful for distinguishing routine work from significant events on the same branch. Tags appear as labels attached to the commit node and are typically used to mark releases.
- `commit id: "name"` — explicit commit id (recommended)
- `commit tag: "v1.0.0"` — adds a release tag
- `commit type: HIGHLIGHT` — visually emphasises this commit
- `commit type: REVERSE` — shows a revert
Merges & cherry-picks
`merge <branch>` performs the most common merge — bring another branch's history into the current one. Add `merge <branch> id: "merge-id"` to label the merge commit, or `merge <branch> tag: "v2.0.0"` to tag it as a release point. Mermaid also supports cherry-picks via `cherry-pick id: "commit-id"`, which brings a single commit from another branch into the current branch without merging the whole history. Cherry-picks are particularly useful in release-branch workflows where a hotfix on `main` needs to be propagated to an older release branch.
Orientation — `LR` for horizontal layouts
By default git graphs render vertically (top-down), with branches as horizontal swim lanes. For wide documentation surfaces — onboarding pages, README files, slide decks — horizontal orientation often reads better. Add `LR` after `gitGraph`: `gitGraph LR:` to flip to left-to-right layout. The same commit / branch / merge syntax works in both orientations; only the rendered direction changes. For most CONTRIBUTING docs that show a typical gitflow, vertical (the default) reads naturally because branches appear stacked.
Common patterns & gotchas
Three patterns dominate real git graphs. Gitflow (feature → develop → release → main with hotfix branches) is the most common — needs at least three branches and several merges to communicate the strategy. Trunk-based development is simpler — most commits land directly on main with short-lived feature branches that merge back quickly. Release-branch workflows show main + release branches per version with cherry-picks for backports. The most common gotcha: forgetting that `checkout` only switches branches, it doesn't create them — use `branch <name>` first if the branch doesn't exist. Second gotcha: representing realistic repository history makes the graph unreadable — pick a representative 5-8 commit slice instead of a 50-commit history.
- `checkout` switches to an existing branch — use `branch` first to create new ones
- Keep commits to a representative slice (5-8); real history clutters
- Tag releases on merge commits: `merge develop tag: "v1.0.0"`