Use a personal access token to access GitLab using SourceTree
Back in August 2018, the SourceTree blog announced that version 3.0 introduced support for a number of different Git hosts based on growing customer demand. One of the most requested hosts was GitLab.com.
This tutorial shows you how to set up SourceTree with GitLab.com using a personal access token.
This option is excellent if you want to access your GitLab.com repositories on your work and personal PC and don’t want to bother managing SSH keys across PCs.
If your GitLab.com account uses Two-factor Authentication (2FA) you must use a personal access token as your password to clone repositories over HTTPS to your local PC environment.
Before you start
This tutorial was written in April 2021 using a MacBook Pro running macOS Big Sur. The procedures for adding an account to SourceTree should apply equally to Windows 10 users, however the procedure has not been tested or verified on this operating system.
Gather the following accounts and other information.
-
An account on GitLab.com
-
Your GitLab.com username. You can find this in your user profile below your name.
-
SourceTree installed with your Atlassian account connected and verified.
Create a personal access token in GitLab
To connect SourceTree with GitLab.com, you can either use SSH or create a personal access token. Personal access tokens let you authenticate with GitLab over HTTPS with 2FA.
To create a personal access token:
-
Load https://gitlab.com/-/profile/preferences to access your user profile settings page.
-
In the side bar, click Access Tokens to load the Personal Access Tokens screen.
-
Add a personal access token by completing the following fields:
- Name
-
Add a short descriptive name for your token. For example,
SourceTree Desktop
. - Expires at
-
Leave the date field blank so the token never expires. Or if you prefer, select an expiry date you feel comfortable with.
- Scopes
-
Select the
api
andread_user
scopes for your token. This allows SourceTree to have full read and write permissions to your repositories and lets the app access your user account settings so it can add this information to commits.Your screen should look like this example.
-
Click Create personal access token.
-
Save the personal access token to a safe place (a secure password manager is recommmended).
This step is the last time you will have access to the personal access token. Make sure you save it now before continuing.
Configure the SourceTree GitLab.com account profile
Now you have created and saved the personal access token, you can configure SourceTree with this token and access your GitLab.com repositories.
To set up a GitLab.com account profile in SourceTree:
-
Open the
screen. -
Click Add… to open the account options screen.
-
Set the following options:
- Host
-
Select
GitLab.com
from the list. - Username
-
Type your username (not your email) into the field.
- Password
-
Paste your personal access token into the field.
- Protocol
-
Select
HTTPS
from the list.
-
Click Save.
-
Close the Accounts screen.
Test the GitLab account by cloning a GitLab repository
To clone repositories from the GitLab.com remote host:
-
In SourceTree, click the Remote option.
-
Expand the GitLab group.
-
Click the Clone link for the repository you want to clone.
-
Check that the Clone a repository modal displays the
https://
prefix in the Source URL field. -
Click Clone to clone the selected repository to your local environment.
Summary
You have configured a GitLab.com account in SourceTree that uses a personal access token to authenticate with GitLab.com