1
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?

More than 1 year has passed since last update.

Githubの使い方 (初期設定からSSH接続)

1
Last updated at Posted at 2024-01-21

前提条件

  • Mac使用
  • GitHubのアカウント作成済み

1. Gitのバージョン確認

git --version

2. Gitの設定

ユーザーネーム、メールドレスを指定

# gitにユーザーを設定する
git config --global user.name “githubユーザーネーム”

# gitにメールアドレスを設定する
git config --global user.email Githubに登録したEmailアドレス
# 成功していれば何も表示されない

gitの設定を確認する

git config user.name
git config user.email
git config --list
# 自分のGithubのアカウント情報が表示されていれば問題なくOK。

2. GitHubにSSHで接続する

GitHubのリポジトリにプッシュするには、SSHで接続する必要がある。公開鍵認証と呼ばれる方式で認証をする。
以下の流れで進める。

  1. 公開鍵の作成
  2. 公開鍵をGitHubに登録
  3. 接続確認

以下のコマンドで公開鍵を作る

ssh-keygen -t rsa -C mail@sample.jp

mail@sample.jpの部分はGithubに登録した自分のメールアドレス

Generating public/private rsa key pair.
Enter file in which to save the key (/home/ec2-user/.ssh/id_rsa): 

上記のコマンドで.sshディレクトリid_rsaというファイル名に自動で設定される。指定のディレクトリと指定のファイル名を使いたい場合は指定する。

Enter passphrase (empty for no passphrase): 

公開鍵にパスワードを設定する。画面には表示されないけれど入力はできている

Enter same passphrase again: 

確認のため同じパスワードを2回入力する。
以下のような表示がされたら完了!

Your identification has been saved in /home/ec2-user/.ssh/id_rsa.
Your public key has been saved in /home/ec2-user/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:■■■■■■■■■■■■■■■■■ mail@sample.jp
The key's randomart image is:
+---[RSA 2048]----+
|   xxxx          |
|xxxxxxxxxxx      |
| x x x xxx  xxx  |
|        xx  x    |
|     xx x        |
|       x x       |
|        x x      |
|         x       |
|          x      |
+----[SHA256]-----+

id_rsaid_rsa.pubの2つのファイルが.sshディレクトリに作成されていることをlsコマンドで確認。

ls ~/.ssh

以下のように表示されたらOK

authorized_keys  id_rsa  id_rsa.pub

公開鍵をGithubに登録

cat ~/.ssh/id_rsa.pubid_rsa.pubの中身を表示させる。
以下のように表示されるので冒頭から全てコピー。

ssh-rsa ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■■■■■■■■■■ mail@sample.jp

Githubに移動。
アイコンを押下して、Settingsを押下。
スクリーンショット 2024-01-22 8.35.26.png

SSH and GPG keysを押下。任意の名前と、先ほどコピーした情報とを貼り付けて最後にNew SSH keyを押下。
スクリーンショット 2024-01-22 8.37.24.png

SSH接続の確認

Terminalで以下のコマンドを入力。

ssh -T git@github.com

初めての接続の際は以下のようなコメントが出るので、yesで。

The authenticity of host 'github.com (13.114.40.48)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
RSA key fingerprint is MD5:16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? 

以下のように表示されたら、設定したパスワードを入力

Warning: Permanently added 'github.com,13.114.40.48' (RSA) to the list of known hosts.
Enter passphrase for key '/home/ec2-user/.ssh/id_rsa': 

以下のように表示されたら成功!!

Hi ユーザー名 You've successfully authenticated, but GitHub does not provide shell access.

but GitHub does not provide shell accと出るから不安になるけど大丈夫みたいです。

これでリモートリポジトリにプッシュができるようになりました!!

1
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
1
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?