LoginSignup
16
9

More than 5 years have passed since last update.

【GitHub】Privateリポジトリの作成から、EC2からの既存ディレクトリpush処理まで

Last updated at Posted at 2016-11-13

久々の投稿です!GitHubのプライベート使えるようにしたので、ちゃんと残そうと思いました。
今回はAPサーバになっているEC2で動いているシステムをGitHub保管するために、
同じEC2内にローカルリポジトリを作成してCommitを行い、
GitHubに新規作成したリモートリポジトリへPushするやり方になります。

前提

  • EC2にGitをインストール済み
  • GitHubのアカウントは取得済み

ssh-keygenで公開鍵、秘密鍵を作る

  • コマンドを入力
[ec2-user@ip-x-x-x-xx]$ ssh-keygen
  • フルパスで秘密鍵になるファイル名を入力。
    ファイル名のみだと、コマンド入力した場所に作成される。
    今回はgithubと名前をつける。
 Generating public/private rsa key pair.
 Enter file in which to save the key (/home/ec2-user/.ssh/id_rsa): /home/ec2-user/.ssh/github
  • 任意のパスフレーズを入力するとキーが作成される
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ec2-user/.ssh/id_rsa): /home/ec2-user/.ssh/github
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ec2-user/.ssh/github.
Your public key has been saved in /home/ec2-user/.ssh/github.pub.
The key fingerprint is:
54:6f:9b:68:b7:af:70:1f:92:12:2a:b7:84:79:ea:0f ec2-user@ip-x-x-x-xx
The key's randomart image is:
+--[ RSA 2048]----+
(省略)
+-----------------+
  • キーが作成されていることの確認 ls -la /home/ec2-user/.ssh/にてgithubファイル(秘密鍵)とgithub.pubファイル(公開鍵)が出来ていることを確認する。
[ec2-user@ip-x-x-x-xx]$ ls -la /home/ec2-user/.ssh/
total 24
drwx------ 2 ec2-user ec2-user 4096 Nov 13 11:05 .
drwx------ 8 ec2-user ec2-user 4096 Nov 13 11:07 ..
-rw------- 1 ec2-user ec2-user  397 Apr 30  2016 authorized_keys
-rw------- 1 ec2-user ec2-user   88 Nov 13 08:01 config
-rw------- 1 ec2-user ec2-user 1766 Nov 13 10:54 github
-rw-r--r-- 1 ec2-user ec2-user  407 Nov 13 10:54 github.pub

GitHubへ公開鍵の登録

  • アカウントのSettingを選択 sample2.jpg  
  • SSH and GPG keysを選択し、New SSH keyを選択 sample3.jpg  
  • 作成した公開鍵をコピーして、貼り付けAdd SSH Keyを選択
[ec2-user@ip-x-x-x-xx]$ cat /home/ec2-user/.ssh/github.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDE0K6pE3FHER5qq8u0dCE77Jm5ZiMtBfPl+pv5OTA7JgAkPcLecxJW6e69oYOVF9vX59gBrP3t6TMbtqQK78uHGFcv5ThH1XQeXdqjc1vFb5JkYDHLs3tnYnapVm3i2UFxygvP5P1qDG0n6xTi3G3c4SuZ/46Bw7phkkZkqYR9VqHtvkQM2vnZz12oHYMDpvfjoeCo7z1vcxuaF/Hh6wHnA3xaWwGDJKM7FARuh7mtrTGq+D0bipkjw8Z/p7zJBhzWCMswR9o6uJfqrV+FBmgURA4kYjdvcvAFEXUlgwUAVx0fe7yYGudxDM4y6W2QGJhvk6whiA5OHOOLfFDvo/Lz ec2-user@ip-x-x-x-xx

sample4.jpg

  • 登録されたことを確認する。 sample5.jpg

configの作成と接続確認

ssh接続の際「~/.ssh/id_rsa」、「~/.ssh/id_dsa」、「~/.ssh/identity」しかデフォルトでは秘密鍵を見に来ません。
今回、鍵を作るときに名前を指定しているため、configファイルを作成する必要があります。

  • vi ~/.ssh/configで以下のコードを入力し、configを作成
Host github github.com
  Hostname github.com
  Port 22
  User git
  IdentityFile ~/.ssh/github
  • パーミッションの強化 作成したままのパーミッションでは以下のエラーが起きる
Bad owner or permissions on /home/ec2-user/.ssh/config

そのため、パーミッションを強化し、オーナーのみが編集出来るようにする。

[ec2-user@ip-x-x-x-xx]$ ls -la ~/.ssh/config
-rw-rw-r-- 1 ec2-user ec2-user 95 Nov 13 12:47 /home/ec2-user/.ssh/config
[ec2-user@ip-x-x-x-xx]$ chmod 600 ~/.ssh/config
[ec2-user@ip-x-x-x-xx]$ ll ~/.ssh/config
-rw------- 1 ec2-user ec2-user 95 Nov 13 12:47 /home/ec2-user/.ssh/config
  • 接続確認をすると、鍵のパスフレーズを聞かれるので入力
[ec2-user@ip-x-x-x-xx]$ ssh -T git@github
Enter passphrase for key '/home/ec2-user/.ssh/github':

以下のメッセージが出たら接続完了

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

GitHub側でもキーの色が変わります
sample6.jpg
 

ローカルリポジトリを作成(既存ディレクトリ)

  • 既存のディレクトリでgit initコマンド
[ec2-user@ip-x-x-x-xx sample]$ pwd
/home/ec2-user/sample
[ec2-user@ip-x-x-x-xx sample]$ git init
Initialized empty Git repository in /home/ec2-user/sample/.git/
  • .gitファイルが作成されていることを確認
[ec2-user@ip-x-x-x-xx sample]$ ls -la
total 16
drwxrwxr-x 3 ec2-user ec2-user 4096 Nov 13 13:13 .
drwx------ 8 ec2-user ec2-user 4096 Nov 13 12:47 ..
drwxrwxr-x 7 ec2-user ec2-user 4096 Nov 13 13:13 .git
-rw-rw-r-- 1 ec2-user ec2-user   11 Nov 13 11:07 sample.txt

 

ローカルリポジトリにコミット

  • git addコマンドでファイルを追加
[ec2-user@ip-x-x-x-xx  sample]$ git add ./*
  • git statusコマンドでコミット待ちファイルの確認
[ec2-user@ip-x-x-x-xx  sample]$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached  < file >..." to unstage)

        new file:   sample.txt
  • git commitコマンドでコミット待ちファイルをローカルリポジトリにコミット
[ec2-user@ip-x-x-x-xx sample]$ git commit -m "first commit"
[master (root-commit) 80d1cdc] first commit

Privateのリモートリポジトリ作成

  • GitHubにリモートリポジトリを作成します。
    • Repository nameの入力
    • Privateを設定
    • Initialize this repository with a READMEにチェックを入れない
    • Create repositoryをクリック sample1.jpg  
  • リモートリポジトリが作成される sample7.jpg

リモートリポジトリへのPush

  • リモートリポジトリを登録する

    • 以下のアドレスをコピーする。 sample8.jpg
  • git remote addコマンドでリモートリポジトリを追加

[ec2-user@ip-x-x-x-xx sample]$ git remote add origin git@github.com:<ユーザ名>/TestRepository.git
  • git remote -vコマンドで追加されたリポジトリの確認
[ec2-user@ip-x-x-x-xx sample]$ git remote -v
origin  git@github.com:<ユーザ名>/TestRepository.git (fetch)
origin  git@github.com:<ユーザ名>/TestRepository.git (push)
  • .git/configの編集 configの作成でHostを指定しているので、urlを変更する
    • git@github.com:<ユーザ名> からgithub:<ユーザ名> に変更
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = github:<ユーザ名>/TestRepository.git
        fetch = +refs/heads/*:refs/remotes/origin/*
  • リモートリポジトリへのPush
[ec2-user@ip-x-x-x-xx sample]$ git push -u origin master
Enter passphrase for key '/home/ec2-user/.ssh/github':
Counting objects: 3, done.
Writing objects: 100% (3/3), 256 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:<ユーザ名>/TestRepository-.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
  • GitHubでPushされたことを確認 sample9.jpg

まとめ

一番ハマったのはデフォルトでは「~/.ssh/id_rsa」、「~/.ssh/id_dsa」、
「~/.ssh/identity」しか秘密鍵が読み込まれないところです。
そのことを全く知らないのに名前付きの鍵を作ってしまいました(;・∀・)
こちらの記事に助けていただきました、ありがとうございます!
gitHubでssh接続する手順~公開鍵・秘密鍵の生成から~

リモートリポジトリ作成時にInitialize this repository with a README
チェックを入れたことでハマりました・・・(;・∀・)
何回やっても! [rejected] master -> master (non-fast forward)がでてしまい、
何度もリポジトリ作り直ししてました><

ようやく既存ディレクトリをローカルリポジトリとしたときに、
リモートリポジトリ作成時にREADME.mdファイルを作ると、その時点で競合が起きることが解りました!
リモートリポジトリからローカルリポジトリにcloneを作る場合は発生しないパターンなので、
すこし調べるのに時間がかかりましたが何とか解消できました!

コマンド部分はもうちょっと調べて、簡素化できるところはまた次回追加していきます!

16
9
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
16
9