やりたいこと
- Githubにsshでpushしたい
- ローカル環境のファイルを,リモート環境に反映させる方法を紹介
公開鍵・秘密鍵の作成
$ mkdir ~/.ssh
$ cd ~/.ssh
$ ssh-keygen
Enter file in which to save the key (/home/mitani/.ssh/id_rsa): # 鍵名
Enter passphrase (empty for no passphrase): # パスワード
Enter same passphrase again: # パスワード(再)
$ ls # ***.pubがあるか確認
$ cat ***.pub # 鍵を確認
ssh-rsa ~ # ssh-rsaを含む文字列をコピー
公開鍵の登録(Web上)
- githubにログイン
- Settingを選択
- SSH and GPG keysを選択
- New SSH keyを選択
- 鍵の名前を付けて,先ほどコピーした公開鍵の文字列をKeyにペーストして"Add SSH key"を押す
$ pwd # パスを確認(後で使用)
$ vi ~/.ssh/config # 秘密鍵を登録(viの使い方は参考で確認)
# 以下を入力
Host github.com
HostName github.com
User git
IdentityFile /home/xxx/.ssh/秘密鍵名 # ***.pubじゃない方(***だけのやつ)
$ ssh -T git@github.com # sshでgithubに接続
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes # yesを入力
Enter passphrase for key '/home/mitani/.ssh/***': # パスワードを入力
Hi masaki130! You've successfully authenticated, but GitHub does not provide shell access. # 接続成功
リモート・リポジトリの作成(Web上)
.gitの作成
$ git init # 以下が出ればok
Initialized empty Git repository in /home/mitani/.git/
ユーザ情報の登録
$ git config --global user.email "you@example.com"
$ git config --global user.name "Your Name"
$ git remote add origin <githubのSSH(git@github.com~)>
$ git remote -v
origin git@github.com:ranohiro/test-git.git (fetch)
origin git@github.com:ranohiro/test-git.git (push)
Add-Push
$ git add test.py # 例としてtest.pyを使用
$ git status # 確認
$ git commit -m "init commit" # コミット
[main (root-commit) 230ddb4] Initial commit
1 file changed, 838 insertions(+)
create mode 100644 test.py
$ git push -u origin main
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 36 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 6.32 KiB | 3.16 MiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:masaki130/work_2026.git
* [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
結果
参考


