Access Gitlab from local using two factor token

How to push changes to Gitlab, when two factor authentication is enabled.

When two factor authentication is enabled in Gitlab, we get authentication error when we try to push changes from our local to Gitlab. The error look like something as shown below,

remote: HTTP Basic: Access denied

To be able to push to Gitlab, we need to setup personal token in Gitlab and set it up in our local Git project as well.

  1. In Gitlab go to User Profile at top right end,

  2. Then select Access Tokens

  1. The Personal Access Token page will open. Here create a personal token. Select all scopes and click Create personal access token

  1. A unique personal access token will be generated. Keep this saved somewhere safe. This will be required in future

  2. Now go to your project and copy the HTTPS GITLAB url.

  1. Now open git bash in your local and type below,
git clone https://oauth2:PERSONAL_ACCESS_TOKEN@gitlab.com/username/project

This should now enable you to push code to Gitlab.

These tokens act as passwords. You can have different tokens set for different projects as well.