この記事について
Macから自分のGithubのレポジトリに変更を加えたく、環境を整備していました。途中でつまづきポイントがいくつかあったので、備忘録がてら整理しようと思います。
環境
macOS: Sequoia 15.0
チップ: M2
1. git cloneを行う
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
SHA256:---
Please contact your system administrator.
Add correct host key in /Users/sophytoeat/.ssh/known_hosts to get rid of this message.
Offending RSA key in /Users/sophytoeat/.ssh/known_hosts:4
Host key for github.com has changed and you have requested strict checking.
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
なぜかsshではうまくいかない模様。一旦HTTPSでgit cloneを行ってみることに。これは上手くいきました。
2. 一旦git pushしてみる
gitのユーザ設定を行う
$ git config --global user.email [Githubに登録したemail-adress]
gitを初期化する
$ git init
git pushを行う
$ git add .
$ git commit -m "test"
$ git push
Githubのユーザーネームとパスワードを入力し、エンターを押すと以下のエラーが発生しました。
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/get-started/getting-started-with-git/about-
remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/~'
どうやらパスワードでの承認は使えないみたいです。ssh接続によるリモート操作に切り替えます。
3. sshキーの生成と登録
sshキーを生成
$ ssh-keygen -t ed25519 -C "your_email@example.com"
生成された公開鍵をクリップボードにコピー
$ pbcopy < ~/.ssh/id_ed25519.pub
Githubにコピーした公開鍵を追加する
GitHubの「Settings(設定)」から「Deploy keys」を選択(Github pageのリポジトリでは「SSH and GPG keys」ではなく「Deploy keys」になっていた)し、「Add deploy key」をクリックします。
※この時に「Allow write access」にチェックを入れるのを忘れないように。忘れるとgit pushの際以下のエラーが出現します。
ERROR: The key you are authenticating with has been marked as read only.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Vimで追記。
以下のコマンドをターミナルへ入力して、「Vim」のテキスト編集画面に入る。
$ vi ~/.ssh/config
編集画面に入ったら下記のような画面が出るので、i
キーで編集モードに切り替える。
~
~
~
~
~
下部が— insert –
と表示されれば編集ができるようになるので下記のコードを一番上にコピペする。
Host github github.com
HostName github.com
IdentityFile ~/.ssh/github
User git
ESC
キーを押してVimの編集モード終了、:wq
と入力することでVimのテキスト保存と画面が閉じられる。
ssh接続をテスト
$ ssh -T git@github.com
以下のエラーが発生。
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
SHA256:uNiVztksCsDhcc0u9e8BujQXVUpKZIDTMczCvj3tD2s.
Please contact your system administrator.
Add correct host key in /Users/sophytoeat/.ssh/known_hosts to get rid of this message.
Offending RSA key in /Users/sophytoeat/.ssh/known_hosts:4
Host key for github.com has changed and you have requested strict checking.
Host key verification failed.
原因はどうやらknown_hostsファイルに古いホストキーが保存されていて、コンピュータが古いホストキー情報を持っているため、新しいホストキーと一致せずエラーが発生するらしい。
4. ssh接続を確立
古いホストキーの削除
Vimを使って古いホストキーを削除する。以下のコマンドでknown_hostsファイルを開く。
~/.ssh/known_hosts
- エラーに示されている行(例:
/Users/sophytoeat/.ssh/known_hosts:4
の4行目)を削除 -
ESC
キーを押してVimの編集モード終了、:wq
と入力することでVimのテキスト保存と画面が閉じられる
ssh接続テスト
$ ssh -T git@github.com
yes/noを選ぶ画面になります。ここはyesを入力しましょう。
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
以下の出力が出れば接続は大丈夫。
no such identity: /Users/sophytoeat/.ssh/github: No such file or directory
Hi sophytoeat/sophytoeat.github.io! You've successfully authenticated, but GitHub does not provide shell access.
githubへのpushを試みる。
$ git push
以下のエラーが発生
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
上記の通りupstreamをセットする。
$ git push --set-upstream origin master
以下が出力される。
branch 'master' set up to track 'origin/master'.
再度git push
を試すと
Everything up-to-date
となり成功。
参考文献
https://kingmo.jp/kumonos/mac-github-ssh-connection/
https://qiita.com/kkrtech/items/a164f455c73867958181
https://qiita.com/ka0ru19/items/1a8ff0fb9e945ef01256