loading...

How to clone a GitLab repository after enabling 2FA

fpeluso profile image Fabio Peluso ・2 min read

I've recently started to enable 2FA (Two Factor Authentication) on most of my online accounts, including GitLab. But what happen when you try to clone a repository against https after that?

When I tried the first time I run the usual git clone:

 git clone https://gitlab.com/username/project.git
Enter fullscreen mode Exit fullscreen mode

but it didn't work, giving me this output:

Cloning into 'mypersonalbalance'...
remote: HTTP Basic: Access denied
remote: You must use a personal access token with 'read_repository' or 'write_repository' scope for Git over HTTP.
remote: You can generate one at https://gitlab.com/profile/personal_access_tokens
fatal: Authentication failed for 'https://gitlab.com/f.peluso/mypersonalbalance.git/'
Enter fullscreen mode Exit fullscreen mode

I googled a little bit, finding that the problems was the 2 factor authentication, and then I found the following solution:

  1. Log in into the GitLab account;
  2. Go to the Settings section (at the moment, top right, hover the profile image and wait for the menu);
  3. Go to Access Tokens;
  4. Now here you can create a temporary token; if you need to pull/push you must enable read_repository and write_repository flags.
  5. A token is shown in the page. Don't close or refresh the page, or you won't be able to retrieve the same token again!!!
  6. Save the token somewhere safe if you need to use it more than once (I needed only once, so I just copypasted it, and then deleted it).

After these steps you can clone the repository using

git clone https://oauth2:PERSONAL_ACCESS_TOKEN@gitlab.com/username/project.git
Enter fullscreen mode Exit fullscreen mode

where PERSONAL_ACCESS_TOKEN is the token you retrieved before.

GitLab explains that the tokens act like passwords after one enables the 2FA, so, in order to improve your security, you can manage one of more tokens to work with different repositories, devices and applications.

Discussion

pic
Editor guide