0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Git】はじめての 「git push」(SSH接続)

0
Last updated at Posted at 2026-01-31

やりたいこと

  • 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を選択
    スクリーンショット 2026-02-01 032825.png
  • 鍵の名前を付けて,先ほどコピーした公開鍵の文字列をKeyにペーストして"Add SSH key"を押す
    スクリーンショット 2026-02-01 032908.png
$ 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上)

  • "New"を押す
    スクリーンショット 2026-02-01 031508.png
  • 以下のように名前や説明を書いて"Create repository"を押す
    スクリーンショット 2026-02-01 021834.png

.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'.

結果

スクリーンショット 2026-02-01 034033.png

参考

0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?