Git for Windows tips

Github.comがpush時に403を吐くので、https->gitとプロトコルを変更して対処する

前記事のプログラム

near finalになったので、Githubにリモートレポジトリを作成し、

git push -u origin main

しようとしたら、下記のエラーが出ました。

IDE内部からpushしても同じです。実はGithubのアカウントは、昔から使っているものと、このブログで使っているものの二つがあり、新しい方でエラーが出ました。同様なことは皆さんよく経験しているようで、

github.com 403 error git push

とかで検索すると色々と出てきます。今回は、結論から言うと、remote repositoryのurlを

https://github.com/alt-doc-nao/alt-filelistbox-modal.git

から

git@github.com:alt-doc-nao/filelist-modal-dialog.git

に変更することで対処しました。ただこうするとIDE内部からのpushは

こう言われて拒否されます。拙作のrmmに含まれている”First push”を使えばおけです。が、まず準備が必要ですが、やり方は、

に詳細に書いてあります。ここではプラットフォームがLinuxなのか不明なので、Git for Windowsだけでやります。できますよ。

$ ssh-keygen -t ed25519 -C "someone@somewhere.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/c/Users/docna/.ssh/id_ed25519):
Created directory '/c/Users/docna/.ssh'.
Enter passphrase for "/c/Users/docna/.ssh/id_ed25519" (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/docna/.ssh/id_ed25519
Your public key has been saved in /c/Users/docna/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:KMzYM06RDYZcmGeThGrrqpxF6v8uNWhMzyzVdF3nL0s jake.burst@gmail.com
The key's randomart image is:
+--[ED25519 256]--+
| . B=.    . .. . |
|  *.=+ . . .  o  |
| . oo.+ .      . |
|.. * o o        .|
|. =.# . S     E .|
| .oB X       . o |
|....+ .       .  |
|o.o.             |
|=+..+o           |
+----[SHA256]-----+

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFXx32VWvHuPLM1j8NE2twj738gTXNWBhZkChZmLq8Mz someone@somewhere.com


$ cat ~/.ssh/id_ed25519.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFXx32VWvHuPLM1j8NE2twj738gTXNWBhZkChZmLq8Mz someone@somewhere.com

docna@Lavie-note MINGW64 ~/OneDrive/ドキュメント/Embarcadero/Studio/Projects/fmx_dd (main)
$ git remote -v
origin  https://github.com/alt-doc-nao/fmx_dd.git (fetch)
origin  https://github.com/alt-doc-nao/fmx_dd.git (push)

docna@Lavie-note MINGW64 ~/OneDrive/ドキュメント/Embarcadero/Studio/Projects/fmx_dd (main)
$ git remote set-url origin git@github.com:alt-doc-nao/fmx_dd.git

docna@Lavie-note MINGW64 ~/OneDrive/ドキュメント/Embarcadero/Studio/Projects/fmx_dd (main)
$ git remote -v
origin  git@github.com:alt-doc-nao/fmx_dd.git (fetch)
origin  git@github.com:alt-doc-nao/fmx_dd.git (push)

docna@Lavie-note MINGW64 ~/OneDrive/ドキュメント/Embarcadero/Studio/Projects/fmx_dd (main)
$ ssh -T git@github.com
The authenticity of host 'github.com (20.27.177.113)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Host key verification failed.

ここだけは明示的に"yes"を入れないとfailed 他ではリターンだけでおけ。

docna@Lavie-note MINGW64 ~/OneDrive/ドキュメント/Embarcadero/Studio/Projects/fmx_dd (main)
$ ssh -T git@github.com
The authenticity of host 'github.com (20.27.177.113)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
Hi alt-doc-nao! You've successfully authenticated, but GitHub does not provide shell access.

ここでブラウザを起動して当該アカウントへSign Inして、web ui経由でpublic keyをよいしょします。

ホームからsettingsへ

左側のメニューから、

上図のようにSSH and GPG keysを選択。

ここで右上の“New SSH key”を選択。

Titleを入れて、Key typeはそのままで、keyへpublic_keyをcatしてcopyしたものを貼りつけ。

これで準備は終了。

$ git push -u origin main
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 8 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 7.09 KiB | 2.36 MiB/s, done.
Total 8 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To github.com:alt-doc-nao/fmx_dd.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

無事終了。このマシンからgithub.comの当該アカウントへのアクセスは、sshで行われるので、ローカルレポジトリが変更されても、リモートがhttpでなく、git…なら動くはずです。urlの確認は、

git remote -v

で。本番に先立って別のリポジトリで試行したものを記録して掲示していますのでご注意ください。適宜本当のレポジトリ名等に読み替えてください。手順を復習すると、

鍵ペアを作成し、秘密鍵の方をweb経由でgithub側に置きます。remote repositoryのurlを変更します。変更するというより上書きするということですね。で、疎通を確認してから、実際にpushしてうまくいけば終わりということなんで、意外に簡単です。当該ディレクトリでGit bashを起動すれば、あとはbash内部で、秘密鍵をgithub.comへ送る部分はブラウザを使うだけです。

コメント