Example git graph code
This is the Mermaid source loaded in the editor above. Copy it into a Markdown file inside a ```mermaid block and GitHub, GitLab, Notion or Obsidian will render it.
gitGraph
commit id: "init"
commit id: "add readme"
branch develop
checkout develop
commit id: "setup ci"
branch feature/login
checkout feature/login
commit id: "login form"
commit id: "auth api"
checkout develop
merge feature/login
checkout main
merge develop tag: "v1.0.0"
commit id: "hotfix" type: HIGHLIGHTGit graph syntax cheat sheet
| Syntax | What it does |
|---|---|
| gitGraph | Start a Git graph. History begins on the main branch. |
| commit id: "msg" | Add a commit. Options: tag: "v1", type: NORMAL | REVERSE | HIGHLIGHT. |
| branch name | Create a branch from the current one and switch to it. |
| checkout name | Switch to an existing branch (switch also works). |
| merge name | Merge the named branch into the current one (can take a tag). |
| cherry-pick id: "x" | Copy a commit from another branch onto the current one. |
How to make a Mermaid git graph
- 1Start the code with gitGraph.
- 2Add commits on the main branch with commit.
- 3Create a branch with branch develop and switch with checkout develop.
- 4Merge back with checkout main then merge develop.
- 5Export as SVG or PNG, or copy a share link.
Commands mirror git
The diagram is a script of git-like commands run from top to bottom: commit adds a commit to the current branch, branch name creates and switches to a new branch, checkout name switches, and merge name merges that branch into the current one.
The graph starts on a branch called main. A branch must exist before you can check it out or merge it.
Commit IDs, tags and types
Give a commit a readable label with commit id: "Add login", and mark releases with commit tag: "v1.0". Merges accept a tag too.
commit type: HIGHLIGHT emphasises a commit and type: REVERSE marks a revert. cherry-pick id: "Add login" copies an existing commit, by its id, onto the current branch.
Diagramming a branching strategy
Git graphs are ideal for documenting GitFlow, trunk-based development or a release process in a README or onboarding guide.
Show only the commits that explain the strategy; a handful per branch is usually enough.