Mermaid Git Diagrams

Git diagrams visualize Git workflows, branches, and commit history. Perfect for documenting branching strategies, release workflows, and Git operations.

Use Case

Use Git diagrams when you need to:

  • Document branching strategies
  • Show Git workflows
  • Visualize commit history
  • Explain merge strategies
  • Design release processes

Code (Basic)

 1```mermaid
 2gitGraph
 3    commit
 4    branch develop
 5    checkout develop
 6    commit
 7    commit
 8    checkout main
 9    merge develop
10```

Result:

Explanation

  • gitGraph - Start Git diagram (note the capital G)
  • commit - Create a commit
  • branch - Create a branch
  • checkout - Switch to a branch
  • merge - Merge branches

Examples

Example 1: Feature Branch Workflow

 1```mermaid
 2gitGraph
 3    commit id: "Initial"
 4    branch develop
 5    checkout develop
 6    commit id: "Setup"
 7    branch feature/login
 8    checkout feature/login
 9    commit id: "Add login"
10    commit id: "Add validation"
11    checkout develop
12    merge feature/login
13    commit id: "Merge login"
14    checkout main
15    merge develop
16    commit id: "Release v1.0"
17```

Result:

Example 2: Git Flow

 1```mermaid
 2gitGraph
 3    commit id: "Initial"
 4    branch develop
 5    checkout develop
 6    commit id: "Dev work"
 7    branch release/v1.0
 8    checkout release/v1.0
 9    commit id: "Prepare release"
10    checkout main
11    merge release/v1.0
12    commit id: "v1.0"
13    branch hotfix/bugfix
14    checkout hotfix/bugfix
15    commit id: "Fix bug"
16    checkout main
17    merge hotfix/bugfix
18    commit id: "v1.0.1"
19    checkout develop
20    merge hotfix/bugfix
21```

Result:

Commands

  • commit or commit id: "message" - Create commit (with optional label)
  • branch name - Create branch
  • checkout name - Switch to branch
  • merge name - Merge branch into current
  • cherry-pick id: "message" - Cherry-pick commit

Notes

  • Commits must have id: label
  • Branch names should be descriptive
  • Use checkout before committing to set branch
  • Merge creates a merge commit

Gotchas/Warnings

  • ⚠️ Commits: Must include id: in commit command
  • ⚠️ Order: Commits must be in chronological order
  • ⚠️ Branches: Create branch before checking out
  • ⚠️ Merges: Must checkout target branch before merging

Related Snippets