0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MacでGithubにgit pushを行うまで

Posted at

この記事について

Macから自分のGithubのレポジトリに変更を加えたく、環境を整備していました。途中でつまづきポイントがいくつかあったので、備忘録がてら整理しようと思います。

環境

macOS: Sequoia 15.0
チップ: M2

1. git cloneを行う

shell
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 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のユーザ設定を行う

shell
$ git config --global user.email [Githubに登録したemail-adress]

gitを初期化する

shell
$ git init

git pushを行う

shell
$ git add .
$ git commit -m "test"
$ git push

Githubのユーザーネームとパスワードを入力し、エンターを押すと以下のエラーが発生しました。

shell
 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キーを生成

shell
$ ssh-keygen -t ed25519 -C "your_email@example.com"

生成された公開鍵をクリップボードにコピー

shell
$ 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の際以下のエラーが出現します。

shell
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」のテキスト編集画面に入る。

shell
$ vi ~/.ssh/config

編集画面に入ったら下記のような画面が出るので、iキーで編集モードに切り替える。

shell
~
~
~
~
~

下部が— insert –と表示されれば編集ができるようになるので下記のコードを一番上にコピペする。

shell
 Host github github.com
   HostName github.com
   IdentityFile ~/.ssh/github
   User git

ESCキーを押してVimの編集モード終了、:wqと入力することでVimのテキスト保存と画面が閉じられる。

ssh接続をテスト

shell
$ ssh -T git@github.com 

以下のエラーが発生。

shell
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    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ファイルを開く。

shell
~/.ssh/known_hosts
  • エラーに示されている行(例:/Users/sophytoeat/.ssh/known_hosts:4 の4行目)を削除
  • ESCキーを押してVimの編集モード終了、:wqと入力することでVimのテキスト保存と画面が閉じられる

ssh接続テスト

shell
$ ssh -T git@github.com 

yes/noを選ぶ画面になります。ここはyesを入力しましょう。

shell
 Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
 Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.

以下の出力が出れば接続は大丈夫。

shell
 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を試みる。

shell
$ git push

以下のエラーが発生

shell
 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をセットする。

shell
$ git push --set-upstream origin master

以下が出力される。

shell
 branch 'master' set up to track 'origin/master'.

再度git pushを試すと

shell
  Everything up-to-date

となり成功。

参考文献

https://kingmo.jp/kumonos/mac-github-ssh-connection/
https://qiita.com/kkrtech/items/a164f455c73867958181
https://qiita.com/ka0ru19/items/1a8ff0fb9e945ef01256

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?