見出し画像

Gitを使いこなすには2

Gitでローカルにリポジトリを作成し、無事ファイルをコミットすることができるようになった。
が、何も考えずにリモートリポジトリへのプッシュのコマンドをそのまま叩いてみたら案の定エラーになりました💦

$ git push origin master
fatal: 'origin' does not appear to be a git repository fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.

ローカルにあるファイルをリモートリポジトで扱えるようにすれば、PJメンバー同士の共同作業を潤滑に行えるし、異なる環境から最新のファイルを取得することも可能となります。

作業の流れは次のようになる
①ローカルリポジトリを作成する
②リモートリポジトリへのプッシュ

リモートリポジトリを作成してみる

GtHub画面


右上Create newボタン


New Repogitoryを選択


リモートリポジトリを作成してSSHを選択

…or push an existing repository from the command line

git remote add origin git@github.com:SatomiTashiro/Automattion_Team.git
git branch -M main
git push -u origin main
ホスト「github.com (20.27.177.113)」の信頼性を確立できません。
ED25519 キーのフィンガープリントは SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU です。
このキーは他の名前では知られていません。
接続を続けてもよろしいですか (はい/いいえ/[指紋])?はい
警告: 既知のホストのリストに「github.com」(ED25519) が永久に追加されました。
git@github.com: アクセス許可が拒否されました (公開鍵)。
致命的: リモート リポジトリから読み取ることができませんでした。

正しいアクセス権があることを確認してください
そしてリポジトリは存在します。
user@CS03432 MINGW64 ~/github/sample (shellscript)
$ git remote add origin git@github.com:SatomiTashiro/Automattion_Team.git

user@CS03432 MINGW64 ~/github/sample (shellscript)
$ git branch -M main

user@CS03432 MINGW64 ~/github/sample (main)
$ git push -u origin main
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.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

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

user@CS03432 MINGW64 ~/github/sample (main)
$ git remote -v
origin  git@github.com:SatomiTashiro/Automattion_Team.git (fetch)
origin  git@github.com:SatomiTashiro/Automattion_Team.git (push)

user@CS03432 MINGW64 ~/github/sample (main)
$ git push origin master
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

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

user@CS03432 MINGW64 ~/github/sample (main)
$ git push origin main  /*エラーになっちゃう!!
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

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


エラーの原因はペア鍵がなかったから

New sskey 左上緑のボタンを押す
user@CS03432 MINGW64 ~/github/sample (main)
$ ssh -T git@github.com
git@github.com: Permission denied (publickey).

user@CS03432 MINGW64 ~/github/sample (main)
$  cd ~/.ssh

user@CS03432 MINGW64 ~/.ssh
$ ls /*ペア鍵があるか確認。結果なかった。
known_hosts

user@CS03432 MINGW64 ~/.ssh
$ ls -al
total 17
drwxr-xr-x 1 user 197609  0 Dec 30 01:28 ./
drwxr-xr-x 1 user 197609  0 Dec 30 01:54 ../
-rw-r--r-- 1 user 197609 92 Dec 30 01:28 known_hosts

user@CS03432 MINGW64 ~/.ssh
$ ssh-keygen -t rsa
Generating public/private rsa key pair.

Enter file in which to save the key (/c/Users/user/.ssh/id_rsa): Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/user/.ssh/id_rsa
Your public key has been saved in /c/Users/user/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:ePlBe8raEguXyljhWT7xFJ/V7XlyLkujeNPSsdR+xFg user@CS03432
The key's randomart image is:
+---[RSA 3072]----+
|               ..|
|          .   . o|
|          .o o ..|
|      ..oo..o ..E|
|     ..=S=o .  O.|
|      =.*o.+  B =|
|     + + ++. * O |
|    . o oo. = = o|
|        .... o  .|
+----[SHA256]-----+

user@CS03432 MINGW64 ~/.ssh
$

user@CS03432 MINGW64 ~/.ssh
$ ls ペア鍵ができた
id_rsa  id_rsa.pub  known_hosts

user@CS03432 MINGW64 ~/.ssh
$ cat id_rsa.pub  ・*ペア鍵をコピーし、GitHub側で登録する
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCiUEe7KDKAFfnKeaxDwXslCKhthmuwn7SjjHefMz8DZb/yH94lRGXDzQhLaG8rUUa0jrsnAxUIeSN4Rz6LlGT8eLgdzKdqHEi61TSMgbU6dolJ6qbktTyCNLleI7Y2z3Sb5ZE1cEBt2p7lsB7L2i38YGXom7BbE/ZGQ6/2whqKfvP4fsQukrQ2gdx/eqkJDHJnRy60opoOjSGATE1mL5QqXrnQIWvX6m3D1uowOhR8RY+vaAVqONv+hw2QSrztL0Fuj/g1/jsOeXbaXOw2WBTlNdVRpO15QMcYtMJF5dzh284OVnyT/mNw2sxMyyz6YKLHrZUk/1gVMT2QX5msjfXBEFLD5t6qiQl4Z9TCABEntu0HyuChoICg6hI8eQLAq3hWxDM4oWeO3KEpIZkiGKw7xM5I3EB+JKFBvSZMKgmcp9/6p/VSzD4z30sTeDNskutDNwt7nYUyt6w2Q12Zgd+G58GZ/QzOYKD2fUKC+hA47OvUaSND77+POcvf9iFyfUM= user@CS03432

user@CS03432 MINGW64 ~/.ssh
$ ssh -T git@github.com
Hi SatomiTashiro! You've successfully authenticated, but GitHub does not provide shell access.

user@CS03432 MINGW64 ~/.ssh

user@CS03432 MINGW64 ~/.ssh
$ cd C:\Users\user\github\sample  /*リモートにプッシュにするにはリポジトリ配下に移動
bash: cd: C:Usersusergithubsample: No such file or directory

user@CS03432 MINGW64 ~/.ssh
$ cd ~

user@CS03432 MINGW64 ~
$ pwd
/c/Users/user

user@CS03432 MINGW64 ~
$ ^C

user@CS03432 MINGW64 ~
$ cd /c/Users/user/github/sample

user@CS03432 MINGW64 ~/github/sample (main)
$ git push -u origin main
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 2 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 728 bytes | 26.00 KiB/s, done.
Total 6 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), done.
To github.com:SatomiTashiro/Automattion_Team.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.


(参考)https://qiita.com/shizuma/items/2b2f873a0034839e47ce




無事リモートリポジトリに登録できました。

祝!リモートリポジトリ登録

この記事が気に入ったらサポートをしてみませんか?