Using Git with Gitlab and second factor auth

PROBLEM: After 2nd factor activation on Gitlab, username and password auth is not working anymore
SOLUTION: Generate API key and use modified url

Generate your API key on Gitlab

You can find this option in Preferences > Access Tokens
Copy this token and note you will see it only once (you have to generate a new one if the old is lost)

Create new URL

https://oauth2:ACCESS_TOKEN@gitlab.com/name/project.git

So if you would like to push or clone the repo using e.g. git clone, it should looks like this

git clone https://oauth2:AB123TOKEN@gitlab.com/name/project.git

Change .git/config

To keep remote origin, adjust file .git/config like this

remote "origin"]
        url = https://oauth2:AB123TOKENN@gitlab.com/name/project.git/
        fetch = +refs/heads/*:refs/remotes/origin/*

or you can create new remote as well

git remote add new-origin https://oauth2:AB123TOKENN@gitlab.com/name/project.git/

but you have to use it when pushing like this

git push new-origin mybranch 

Working with IntelliJ IDEA/Webstorm

Just go to VCS > Git > Remotes… and change the URL of origin or add new remote and you should be fine 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *