Reputation: 4764
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.
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...
I had many repos on local before I enabled the 2nd-factor authentication. I want these to work too.
Upvotes: 122
Views: 86240
Reputation: 137574
A user-friendly alternative to personal access tokens is Git Credential Manager which does secure OAuth authentication via web browser. Documentation at https://docs.gitlab.com/ee/user/profile/account/two_factor_authentication.html#git-credential-manager
Upvotes: 0
Reputation: 69
See the link below enter link description here
You just need to create a new token for your profile! To do this,
Upvotes: 5
Reputation: 451
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.
Upvotes: 45
Reputation: 3823
Visit the below link and enter your Name and Expiry Date.
Then click on the different checkboxes like read_user, read_repository, write_repository, etc for access scopes and create a new Personal Access Token and store it in a secured location
https://gitlab.com/profile/personal_access_tokens
Now when you do a git pull, git clone, git push, etc you can enter your username/email as the Username and enter the newly created Personal Access Token as Password
Upvotes: 6
Reputation: 715
Clone current repo with git clone ${CI_REPOSITORY_URL}
Clone other repos with git clone https://oauth2:${PERSONAL_ACCESS_TOKEN}@gitlab.com/acme/my-project.git
. Gitlab uses "oauth2" + token convention to populate OAuth2 Authentication headers, but I could not find official documentation for this.
Enable current repo modifications with git remote set-url origin ${CI_PROJECT_URL/gitlab.com/oauth2:${PERSONAL_ACCESS_TOKEN}@gitlab.com}.git
Here is a job that tags the current repo, using git push:
build_rpms:
stage: package
script:
- echo "Build RPMs. Add tag v1.9d"
- apk add git
- git config --list
# --force is needed for both tag and push to allow job replay
- git tag v1.9d --force
# Enable pushing from CI pipeline:
#
# At that point git origin points to CI_REPOSITORY_URL=
# https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/acme/my-project.git
# This setup does not allow modifications (i.e git push will be rejected).
#
# We use Gitlab Personal Access Token with 'write' access. This token shall
# be generated via Gitlab user settings and then it shall be added as a masked
# environment variable for this project CI settings.
#
# Use "oauth2" as user. For example for CI_PROJECT_URL=https://gitlab.com/acme/my-project
# set origin to https://oauth2:[email protected]/acme/my-project.git
#
- git remote set-url origin ${CI_PROJECT_URL/gitlab.com/oauth2:${PERSONAL_ACCESS_TOKEN}@gitlab.com}.git
- git remote -v
# Use -o ci.skip option to avoid triggering pipeline again
- git push origin v1.9d --force -o ci.skip
when:
manual
Upvotes: 0
Reputation: 249153
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.
Upvotes: 247