How to use git commands after enable gitlab's second-factor authentication

an offer can't refuse picture an offer can't refuse · Aug 2, 2018 · Viewed 13.6k times · Source

Today I've enabled Gitlab's 2nd-factor authentication. After that, since I logged in the Gitlab website, I need to use my cell phone to pass a 6-digits plus my password, that's good, it makes me feel safe.

However, when I use the general operations, for example git clone some-repo.git, I got the error:

Cloning into 'some-repo'...
remote: HTTP Basic: Access denied
remote: You must use a personal access token with 'api' scope for Git over HTTP.
remote: You can generate one at https://gitlab.com/profile/personal_access_tokens
fatal: Authentication failed for 'some-repo.git'

Then I try existing cloned local repo, using git pull, the same error occurs. Before I enabled the 2nd-factor authentication, all the above operation worked fine.

Flowing the above error's instructions, I went to the mentioned address: https://gitlab.com/profile/personal_access_tokens. I created the following token, and save the token's key.

enter image description here

However, I don't know what to do with this key. Can someone tell me how to use this key to enable the basic operations like git pull, git clone, git push etc...

Edit

I had many repos on local before I enabled the 2nd-factor authentication. I want these to work too.

Answer

John Zwinck picture John Zwinck · Aug 5, 2018

As explained in using gitlab token to clone without authentication, you can clone a GitLab repo using your Personal Access Token like this:

git clone https://oauth2:[email protected]/yourself/yourproject.git

As for how to update your existing clones to use the GitLab Personal Access Token, you should edit your .git/config file in each local git directory, which will have an entry something like this:

[remote "origin"]
    url = https://[email protected]/yourself/yourproject.git

Change the url:

[remote "origin"]
    url = https://oauth2:[email protected]/yourself/yourproject.git

Now you can continue using this existing git clone as you did before you enabled 2FA.