After I enabled the “two-factor authentication“ on GitLab, I got the following error message when pushing changes to the remote Git repo:
remote: HTTP Basic: Access denied
remote: You must use a personal access token with 'read_repository' or 'write_repository' scope for Git over HTTP.
remote: You can generate one at https://gitlab.com/profile/personal_access_tokens
fatal: Authentication failed for 'https://gitlab.com/user/project.git/'
After some research I realize some changes should be done to make it work.
First, you need to create an access token with the “read_repository/write_repository” permissions in GitLab. Then add the token into your remote repo’s HTTPS URL as below:
# 1. Remove the remote item first.
git remote remove origin
# 2. Add it again with the token.
git remote add origin https://oauth2:ThIsiStHeToKeNcOnTeNt@gitlab.com/user/project.git
# 3. Set upstream branch.
git branch --set-upstream-to=origin/master master
After that, I can push the changes successfully without “username/password” required.