Skip to main content

This version of GitHub Enterprise Server will be discontinued on 2026-09-22. Discontinued releases are not supported. No patch releases will be made, even for critical security issues. For better performance, improved security, and new features in GitHub Enterprise Server, see Overview of the upgrade process. For help with the upgrade, GitHub Enterprise Support.

Annotating code examples

You can annotate longer code examples to explain how they work and how people can customize them for other uses.

About code annotations

Code annotations help explain longer code examples by describing what a code example does and why. The annotations render next to the code example in a two pane layout, so we can write longer annotations without making the code itself difficult to read. We only annotate full code examples, not snippets. Code annotations are not required for every code example and should only be used if there is a clear need for them.

Code annotations can be helpful for a variety of audiences. Often, code annotations will be used to explain key concepts to new users or specific choices to more experienced users.

For new users, code annotations are a way to go beyond the high level overview of a code example and explain what each line of code does so that someone can understand the code as if a friend or coworker were guiding them through it.

For more experienced users, code annotations can help them understand a code example and then tailor it to their specific needs. Annotations can explain why code was written a certain way so that the fundamentals are clear.

You can annotate multiple code examples in a single article, but keep in mind that each annotation increases the complexity of an article and adds repetitive navigation tasks for people using screen readers. If you have multiple code examples in an article, consider whether they can be combined into a single example.

Enabling and adding code annotations

  1. Specify the layout: inline frontmatter property for the article.
  2. Create a code example using triple backticks.
  3. Specify a language for the code example after the triple backtick, followed by annotate. For example, ```yaml annotate or ```ruby annotate.
  4. Add annotations using comment tags (#, //, <&#33--, %%) within the code example. You must use the comment tag for the language that the code sample is written in. For example, # for YAML and // for JavaScript.
    • An annotated code example must start with a single line annotation. You can start with a blank annotation if you do not want to add an annotation to the first line of code.
    • Annotations apply to the code from the line below the comment tag to the next comment tag or the end of the code block.

Annotation rules

The following rules apply to all code annotations.

  • Multiline-style comments, such as /* are not supported.
  • There must be a space between the symbol that starts a code annotation and the comment.
    • Use: # comment
    • Avoid: #comment
  • To create a blank annotation, insert a comment tag with no text after it. Blank annotations are useful if some lines of a sample don't require an annotation.
  • Strings that begin with #! will render in the code block and are not treated as comments.
  • Anything after the comment tag will be parsed with Markdown. Links, versioning, and other styling will render as if they were written in Markdown.