How to create and use GitLab Snippets

Although GitLab introduced Snippets in version 12.6, I had never taken advantage of this feature until recently. I found myself in a situation where I needed to share a script across multiple projects, and that’s when Snippets came to the rescue.

How to create

Creating a Snippet is a breeze. Simply access the GitLab web interface and look for the small “+” icon with a down arrow at the top-right corner of the page. Clicking on it will reveal a menu, from which you can select “New Snippet.”

Alternatively, you can navigate to the main GitLab menu on the web and choose “Snippets” from there.

This will take you to the Snippets page, where you can see all the existing Snippets. To create a new one, click on the “New Snippet” button located at the top-right corner.

Once you’re on the “New Snippet” page, you can enter your Snippet code and provide additional details. This includes specifying a Title, an optional Description, a File name with extension for proper syntax highlighting, and the script/code/text lines. Additionally, you can set the visibility level for the Snippet. Finally, click “Create Snippet” to create your Snippet. It couldn’t be easier!

How to access

Accessing your Snippet is straightforward. After creating it, you’ll find it listed on the Snippets page, along with the title you specified during creation. Under the name, you’ll see the Snippet, the author’s name, and the creation date.

Similar to any GitLab project, you can clone a Snippet using either SSH or HTTPS. To clone via HTTPS, use the following format:

The HTTPS format to clone would be –

https://<your-gitlab-address>/snippets/<snippet-number>.git

To access the Snippet in code or script, use this format:

https://<your-gitlab-address>/snippets/<snippet-number>

If you only need to access the content of the Snippet, use the following format:

https://<your-gitlab-address>/snippets/<snippet-number>/raw

With GitLab Snippets, sharing and reusing code across projects has never been easier. Give it a try and streamline your code collaboration process today!

Leave a Reply

Your email address will not be published. Required fields are marked *