Share and Use Code Snippets
A Snippet hosts reusable code in files that you can use in the project and share with other project members. The file could include a small block of reusable source code or text that can be incorporated into larger modules.
Content in snippet files may not be code, but it should be useful. This content could be notes that you want to share with project members, or something you want to keep private, such as a reminder to yourself. If a snippet is shared, project members can copy or download the snippet files and then use them in their own applications.
A snippet can contain several files. When you create a snippet, you can add one file only, but you can add additional files after creating the snippet.
Create and Manage Snippets
You can create a snippet from the Snippets page or from a text selection in a code editor. You can only add one file when you create the snippet, but you can add more files later.
Action | How To |
---|---|
Create a snippet |
|
Create a snippet from a selection |
You can create a snippet from a file that’s open in the code editor.
|
Share or stop sharing a snippet |
After creating a snippet you may want to share it with your team members, or stop sharing it if it is already shared. You can set the share status of a snippet that you own. To share a snippet, in the My Snippets view of the Snippets page, click Share . The icon changes to Shared . To stop sharing a snippet, click Shared . The icon changes to Private . |
Edit a snippet’s title |
You can edit the title of a snippet that you own. On the Snippets page, click the snippet name, and then click Edit . |
Delete a snippet |
You can delete a snippet that you own. On the Snippets page, click the snippet name, and then click Delete. In the Delete Snippet dialog box, click Yes to confirm. |
Add and Manage Files of a Snippet
You must be the creator of the snippet to add or manage its files.
Action | How To |
---|---|
Add a file |
|
Edit a file |
|
Delete a file |
|
Copy Contents of a Snippet File
You can copy contents of a snippet file manually from the Snippets page or insert it from the context menu in the code editor component.
The code editor is available in various editing pages and input fields such as the Edit Wiki page, edit mode of the readme file, snippet file editor, merge request comment box, and the shell command box of the Configure Build page.
Action | How To |
---|---|
Copy from the Snippets page |
|
Insert from the context menu |
|
Add a Comment to a Snippet
Add a comment to a snippet to share information with other developers.
Use Git with Snippets
You can use Git to clone a snippet repository and manage its files. After you clone the snippet’s repository, you can view the file history, and update and commit files locally. However, you can only push updates to repositories for snippets that you own.
- Open the snippet.
- On the Snippet Details page, at the top, click Clone, and then click Copy to copy the HTTP or the SSH URL of the snippet repository.
- Use Git commands to clone the repository, update files, and push the commits to the project.
Download an Archive of the Snippet
You can download a zip or a tgz file of the snippet to your computer. You may want to do this if you back up the files of the snippet before deleting it.
- Open the snippet.
- On the Snippet Details page, at the top, click Clone, and the select Download ZIP or the Download TGZ option.