Recently I need to checkout a new repo from a self-hosted Gitlab server. I did the usual thing:
- Generate a pair of ssh keys using
ssh-keygen(since I'm on a new Windows machine). If you already have the ssh keys (try ls in your ~/.ssh and you see
id_rsa.pub), then you don't need to generate the key pairs again.
- Add it to the SSH key section of Gitlab
But still, I got the error:
Normally, it should work, right? Yes, I've done this countless time with GitHub, Gitlab, Bitbucket... and had no issue.
By the way, the account has 2FA enabled.
I don't know the reason for this but I have a workaround.
Access to Gitlab using personal access token
The workaround is to use Gitlab's personal access token. I haven't tried this on Github (since it works perfectly with SSH key) but I guess it should work too.
The first step is to generate a personal access token:
Then, enter the name of the token and check
Then click on Create personal access token and you will get a token to use:
Now, head back to your repository and copy the standard https URL. It should look something like this:
You need to modify that URL a bit using your newly created token:
Now, I can use that url to clone the repo:
In case you already have the repo, you just need to update the remote url
git remote set-url origin https://<token_name>:<token>@git.some-server.com/group/repo/repo-name.git
Then you can pull, push normally.
That's it! You can continue working on your repo again. I still don't know why adding ssh didn't work but using the personal access token is an acceptable work around.
One thing I don't like about this method is any one who can access to your machine can run:
git remote -v
And see your access token. That person then can use the credentials to do what you can on your repo.
So, lock your screen when you are not at your computer :D
If you enjoy this post, make sure to check my blog out at https://datmt.com. I wrote about #programming stuffs there :)
Top comments (1)
Thanks, this solution helped fixing the issue.