LoginSignup
0
0

GitHubにHTTPSで接続する(Windows, Git Credential Manager)

Last updated at Posted at 2023-12-30

1. はじめに

githubを久しぶりに利用しようと思ったのですが、HTTPSでのpushでつまづいたため、その対応を記録するついでに、設定方法をメモしておきます。

2. 概要

  • githubのリポジトリへコードをpushする
  • githubへの接続は「HTTPS」を利用し、「ssh」は使用しない
  • HTTPS接続では、「トークン」を発行して接続する
  • 「トークン」は端末に保存して使用する

3. 確認した環境

  • Windows10
  • Powershell
  • git version 2.43.0.windows.1

4. GitHubのリポジトリ作成

リポジトリ作成

Privateで作成しました。
image.png

githubリポジトリの作成後画面
image.png

5. 表示されたコマンドの実行

リポジトリ作成時に案内されるコマンドは以下になります。

echo "# (リポジトリ名)" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/(githubのID)/(リポジトリ名).git
git push -u origin main

表示されたコマンドをそのままの順で実行すると以下のエラーがでました。
(HTTPSで接続したはずなのに、なぜssh?)

> git push -u origin main
ssh: Could not resolve hostname github: Name or service not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

git remoteの状況を確認します。pushの方がHTTPSの記載にはなっていませんでした。

> git remote -v
origin  https://github.com/(githubのID)/(リポジトリ名).git (fetch)
origin  github:(githubのID)/(リポジトリ名).git (push)

.gitconfigにて以下の記述を入れていたことが原因のようでした。なぜいれたのかは記憶がなく、接続方式を変換するための記述のようでしたので、この記述を消しました。

[url "github:"]
    pushInsteadOf = https://github.com/
    pushInsteadOf = git@github.com:

一度、remoteの設定を消して、再度設定します。両方、HTTPSのURLになりました。

> git remote rm origin
> git remote add origin https://github.com/(githubのID)/(リポジトリ名).git.git
> git remote -v
origin  https://github.com/(githubのID)/(リポジトリ名).git (fetch)
origin  https://github.com/(githubのID)/(リポジトリ名).git (push)

再度pushしようとすると以下のようにサインインが表示されました。
(ここでは一度中断しておきます。)
image.png

6. GitHubのアクセストークン発行・保管・削除方法

HTTPS接続にはアクセストークンの発行が必要なので、GitHubの画面から発行します。

トークンの発行

「Settings」⇒「Developper Settings」から「Personal access tokens」を選択し、「Generate new token」から発行します。
image.png

NoteとExpiration(有効期限)を入れます。スコープ(Select Scopes)は最低限必要な「repo」と「workflow」、「admin:org」にある「read:org」にチェックを入れて作成します。(他にも必要であれば追加してください。)
image.png
image.png

トークンが発行されるので、コピーしておきます。(ここでしか表示されないので要注意)
image.png

左メニューのTokens(classic)を再度押すと、以下のように発行したものが表示されます。
image.png

認証情報の保管場所と削除方法

認証情報を端末に保管する方法はいくつかあるようですが、Git Credential Managerで保存することにしました。
https://docs.github.com/ja/get-started/getting-started-with-git/caching-your-github-credentials-in-git

gitをいれた時に入っていたようです。

> git credential-manager --version
2.4.1
> git config --show-origin credential.helper
file:C:/Program Files/Git/etc/gitconfig manager

利用方法

初回にGitHubの操作を行った際、サインインが出るので、Tokenを選択して発行したトークン文字列をいれます。
image.png

以降、こちらの認証情報が使用されるようです。

認証情報が保存されている場所

認証情報はWindowsの資格情報マネージャー(Windows Credential Manager)に保管されているようです。
image.png
Windows資格情報の方にあります。
image.png

汎用資格情報の中にgithubの認証がありました。こちらの登録を削除すると再認証が必要になります。
image.png
image.png

0
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
0
0