LoginSignup
1
0

More than 1 year has passed since last update.

Gitのリモートリポジトリとして個人用アクセストークンが必要なリモートリポジトリを複数設定する方法

Last updated at Posted at 2021-05-18

概要

Gitのリモートリポジトリとして個人用アクセストークンが必要なリモートリポジトリを複数設定する方法について説明します。
例えば1つがAzure DevOps Services、もう1つがAzure DevOps Serverという場合に必要な手順となります。

手順

  1. リモートリポジトリを追加します

    git remote add origin <RepoURL1>
    git remote add foo <RepoURL2>
    
  2. gitリポジトリに接続先のURLを含めたhttp.extraHeaderを指定します

    git config http.<RepoURL1>.extraheader "Authorization: Basic ${Your_B64_PAT_On_RepoURL1}"
    git config http.<RepoURL2>.extraheader "Authorization: Basic ${Your_B64_PAT_On_RepoURL2}"
    
Your_B64_PAT_On_RepoURL1 <RepoURL1>に接続するためのBase64にデコードしたパーソナルアクセストークン。後述の手順で生成する
Your_B64_PAT_On_RepoURL2 <RepoURL2>に接続するためのBase64にデコードしたパーソナルアクセストークン。後述の手順で生成する

説明

Azure DevOps Services、Azure DevOps Server で個人用アクセストークン(PAT)を利用してクローンする方法として、PATからBase64にデコードしたトークンを生成しhttp.extraHeaderを指定する方法が公式ドキュメントに示されています。

MY_PAT=yourPAT      # replace "yourPAT" with your actual PAT
B64_PAT=$(printf "%s"":$MY_PAT" | base64)
git -c http.extraHeader="Authorization: Basic ${B64_PAT}" clone https://dev.azure.com/yourOrgName/yourProjectName/_git/yourRepoName

認証情報としてリポジトリにhttp.extraHeaderを設定することで、push,fetch時にも認証することができます。

git config http.extraHeader "Authorization: Basic ${B64_PAT}"

しかし上記の方法では複数リモートリポジトリがある場合に対応できないため、URLごとのhttp.extraHeaderを設定する必要があります。

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0