Table of Contents
はじめに
GitLabで2段階認証を設定した後に、git cloneしたところ、以下のエラーが出るようになります。
今回はその解決方法についてまとめます。
remote: HTTP Basic: Access denied. If a password was provided for Git authentication, the password was incorrect or you're required to use a token instead of a password. If a token was provided, it was either incorrect, expired, or improperly scoped. See https://gitlab.com/help/topics/git/troubleshooting_git.md#error-on-git-fetch-http-basic-access-deniedfatal: Authentication failed for 'https://gitlab.com/username/repository.git/'解決策
パスワードの代わりにアクセストークンを設定してアクセスすることが必要です。
1. パーソナルアクセストークンの取得
ユーザー設定 > Access tokens から新しいPersonal access tokenを作成する
- トークン名: 適当に設定
- 有効期限: 適当に設定。空欄の場合は、1年になる。
- スコープ: read_repository,write_repositoryあたりにチェック
2. パーソナルアクセストークンの設定
- credential.helperを使用する。
# `末尾をstore`とした場合、認証情報をディスク上に平文で保存する。# 一方、`cache`とすれば、メモリ上に一時的に保存する。$ git config --global credential.helper store- git pushする。
# 最初のプッシュ時に認証情報を要求されるため、ユーザー名とパスワード/トークンを入力する。# 認証情報が保存され、次回からは自動的に使用されます。$ git push origin main# -> Username: ユーザー名# -> Password: トークン
# ~/.git-credentials には以下のような形式で保存# https://ユーザー名:トークン@gitlab.comおまけ
リモートURLにトークンを含めることも可能。
ただし、リモートURLにトークンを含めると、そのURLが.git/configに保存されるため注意。
# 既存のリモートURLを削除git remote remove origin
# パーソナルアクセストークンを含む新しいURLを追加git remote add origin https://oauth2:トークン@gitlab.com/ユーザー名/リポジトリ名.git