doc/user/snippets.md
{{< details >}}
{{< /details >}}
With GitLab snippets, you can store and share bits of code and text with other users. You can comment on, clone, and use version control in snippets. They can contain multiple files. They also support syntax highlighting, embedding, downloading, and you can maintain your snippets with the snippets API.
You can create and manage your snippets with:
glab CLI.GitLab provides two types of snippets:
On GitLab.com, group Owners can restrict personal snippet creation for enterprise users.
For project snippets, the project's visibility always takes precedence over the snippet's visibility setting. A snippet marked as public is not accessible to anyone who cannot already access the project.
| Project visibility | Who can access public snippets | Who can access private snippets |
|---|---|---|
| Private | Project members only | Project members only |
| Internal | Authenticated users (except external users) | Project members only |
| Public | Everyone | Project members only |
[!note] On GitLab.com, the
Internalvisibility setting is disabled for new projects, groups, and snippets. Existing snippets using theInternalvisibility setting keep this setting. For more information, see issue 12388.
For personal snippets, the snippet's visibility setting controls access directly:
You can create snippets in multiple ways, depending on whether you want to create a personal or project snippet:
glab CLI, using the
glab snippet create command.
For full instructions, see the command's documentation.Gitlab: Create snippet command.example.rb or index.html.
Filenames with appropriate extensions display syntax highlighting.
Failure to add a filename can cause a known
copy-pasting bug.
If you do not provide a filename, GitLab creates a name for you.For project snippets, the project's visibility acts as the outer boundary. For more information, see snippet visibility.
After you create a snippet, you can still add more files to it. Snippets are versioned by default.
To discover all snippets visible to you in GitLab, you can:
View a project's snippets:
View all the snippets you created:
On GitLab.com, you can also visit your snippets directly.
Explore all public snippets:
On GitLab.com, you can also visit all public snippets directly.
Project snippets are enabled and available by default. To change their default visibility:
Both personal and project snippets use version control by default.
This means that all snippets get their own underlying repository initialized with a default branch at the moment the snippet is created. Whenever a change to the snippet is saved, a new commit to the default branch is recorded. Commit messages are automatically generated. The snippet's repository has only one branch. You can't delete this branch, or create other branches.
Snippets support syntax highlighting based on the filename and extension provided for them. You can submit a snippet without a filename and extension, but a valid name is required for creating content as a file in the repository.
If no filename and extension are provided for the snippet,
GitLab adds a filename in the format snippetfile<x>.txt
where <x> represents a number added to the file, starting with 1. This
number increments if you add more unnamed snippets.
When upgrading from an earlier version of GitLab to 13.0, existing snippets
without a supported filename are renamed to a compatible format. For
example, if the snippet's filename is http://a-weird-filename.me it is
changed to http-a-weird-filename-me to be included in the snippet's
repository. As snippets are stored by ID, changing their filenames breaks
direct or embedded links to the snippet.
A single snippet can support up to 10 files, which helps keep related files together, such as:
docker-compose.yml file and its associated .env file.gulpfile.js file and a package.json file, which together can be
used to bootstrap a project and manage its dependencies.If you need more than 10 files for your snippet, you should create a wiki instead. Wikis are available for projects at all subscription levels, and groups for GitLab Premium.
Snippets with multiple files display a file count in the snippet list:
You can manage snippets with Git (because they're versioned by a Git repository), through the snippets API, and in the GitLab UI.
To add a new file to your snippet through the GitLab UI:
To delete a file from your snippet through the GitLab UI:
To ensure you receive updates, clone the snippet instead of copying it locally. Cloning maintains the snippet's connection with the repository.
To clone a snippet:
You can commit changes to a cloned snippet, and push the changes to GitLab.
Public snippets can be shared and embedded on any website. You can reuse a GitLab snippet in multiple places, and any change to the source is reflected in the embedded snippets. When embedded, users can download it, or view the snippet in raw format.
To embed a snippet:
Confirm your snippet is publicly visible:
In your snippet's Embed section, select Copy to copy a one-line script you can add to any website or blog post. For example:
<script src="https://gitlab.com/namespace/project/snippets/SNIPPET_ID.js"></script>
Add your script to your file.
Embedded snippets display a header that shows:
For example:
<script src="https://gitlab.com/gitlab-org/gitlab-foss/snippets/1717978.js"></script>You can download the raw content of a snippet. By default, they download with Linux-style line endings (LF). If
you want to preserve the original line endings you must add a parameter line_ending=raw
(For example: https://gitlab.com/snippets/SNIPPET_ID/raw?line_ending=raw). In case a
snippet was created using the GitLab web interface the original line ending is Windows-like (CRLF).
With snippets, you engage in a conversation about that piece of code, which can encourage user collaboration.
{{< details >}}
{{< /details >}}
Administrators on GitLab Self-Managed can mark snippets as spam.
Prerequisites:
To do this task:
GitLab forwards the spam to Akismet.
Because versioned snippets are considered as part of the namespace storage size, it's recommended to keep snippets' repositories as compact as possible.
For more information about tools to compact repositories, see the documentation on reducing repository size.
If the text area after the filename field is disabled and prevents you from creating a new snippet, use this workaround: