Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 421
an offer can't refuse Avatar asked Aug 02 '18 16:08

an offer can't refuse


People also ask

How do I use Git 2 factor authentication?

In the upper-right corner of any page, click your profile photo, then click Settings. In the "Access" section of the sidebar, click Password and authentication. Under "Two-factor authentication", click Enable two-factor authentication. Under "Two-factor authentication", select Set up using SMS and click Continue.

What is enforced 2FA?

all tiers. Two-factor authentication (2FA) provides an additional level of security to your users' GitLab account. When enabled, users are prompted for a code generated by an application in addition to supplying their username and password to sign in.


2 Answers

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.

like image 64
John Zwinck Avatar answered Sep 20 '22 04:09

John Zwinck


I used the generated Personal Access Token as the password when prompted to enter credentials.

This allowed me to just use the standard Git Clone syntax without entering anything additional.

When you generate, copy the token. This is the password that will be stored in Credential Manager when you clone. Use that as your password instead of your git password.

like image 30
Erik Avatar answered Sep 20 '22 04:09

Erik