LoginSignup
6
9

More than 5 years have passed since last update.

githubからサーバへのプログラムの設置

Last updated at Posted at 2017-03-08

github上のプログラムのソースコードをサーバへ設置するときの手順です.

publicなリポジトリの場合は鍵ファイルの用意は不要です.
リポジトリパスのコピー の説明通りにパスをコピーしてcloneすれば終わりです.

※ 前提条件

  • サーバ上にgitがインストール済み
  • githubのリポジトリの管理権限を持っている
  • 対象となるリポジトリはprivateリポジトリ

サーバ側の用意

鍵ファイルの作成

デプロイ対象のサーバにログインし,鍵を作成します.
設置場所は通常の公開鍵と同じく,自分のホームディレクトの下の .ssh ディレクトリの中です.
/home/user/.ssh

.ssh ディレクトリがない場合は作成し,パーミッションを700に設定.

mnu@server:~$ mkdir .ssh
mnu@server:~$ chmod 700 .ssh

鍵の名前はリポジトリ名に合わせましょう.
パスワードを聞かれますが,入れなくていいです.

後述しますが,githubでは別のリポジトリに登録した公開鍵を使いまわしすることができないので
githubという名前にしちゃうと複数のリポジトリからソースコードを落とそうとした時に困ることになります.

mnu@server:~$ cd .ssh
mnu@server:~/.ssh$ ssh-keygen
mnu@server:~/.ssh$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/mnu/.ssh/id_rsa): tutorial
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in tutorial.
Your public key has been saved in tutorial.pub.
The key fingerprint is:
SHA256:TUTLNx9hpBoEDGqtxaqh8N5W/A064RfEsmJft/mAIsI mnu@tutorial.usa-mimi.jp
The key's randomart image is:
+---[RSA 2048]----+
|      .o.o+  .+  |
|     +  .+ . o . |
|    o +.  = + .  |
|   . +. oo + o . |
|. . o. +S o   .  |
|.+ oo = +..      |
|. E..=.=.=.o     |
| . o..=.o +.     |
|  ...  o   ..    |
+----[SHA256]-----+

設定の記述

.ssh/config に作成した鍵を使うための設定を書きます.

~/.ssh/config
Host tutorial.github.com
    HostName github.com
    IdentityFile ~/.ssh/tutorial
    IdentitiesOnly  yes
    User    git

Host は識別名です.なんでもいいですが, リポジトリ名.github.com にしとくとわかりやすいです.
IdentityFile には先ほど作成した秘密鍵を指定します.
※ githubにアクセスするときはUserは常にgitです.

公開鍵のコピー

公開鍵をコピーします.
catコマンドなどでconsoleに出してコピーすればOKです.

mnu@server:~/.ssh$ cat tutorial.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDYWh553wK1/ZPWp64vjPO3m0VbAquzeiMtIBJNPl3QdF7k68tTnNvzlg7kdwnCV8a5NmyoS6+ZWC5ycBf0s2/223M5M/9YdXTXY3PIgDTz03QDhWuNB11lXv2YcoNs7REIb6rrEGebSfeFMVqAm6elZFFldHE/l+QjQZpXJqOo9oGbFUpo7uJOe+SOIsP/Mh+JALAzswqPKZN6Uq8wWxP0Bunon5n/5x8MzDV0o1vNaGI4ARF7w0bMMeClaUAfp7to2v2g78P4cDwkZFHv6wE255OxIArlZjhcumfJIueNVvmMfL9RflCb7OOkN6kbJ+LVeH4Eoh9WB9TvZhqCShvH mnu@tutorial.usa-mimi.jp

github側の用意

公開鍵を登録します.
githubにログインし,対象のリポジトリに移動の上,
SettingsDeploy keysAdd deploy key の順にクリック

Kobito.hTsJpB.png

Kobito.HSnYKo.png

keyの中に先ほどコピーした公開鍵をペースト.
titleにはサーバ名とかを入れとくと後でわかりやすいです.

Kobito.S4pKe7.png

Add key を押すとgithubのパスワードを聞かれるので入力.
成功するとこんな感じで登録されます.

Kobito.W5THS5.png

リポジトリパスのコピー

鍵が登録できたら,最後にリポジトリへのパスをコピーします.

  1. リポジトリのコードを表示
  2. clone or download ボタンをクリック
  3. Clone with HTTPS 担っている場合は Use SSH をクリックして切り替え

    Clone with SSH になっている場合は不要

  4. ここをクリックするとコピーできる.上手くコピー出来ない場合は中の文字列を選択してコピー

Kobito.6xO1PG.png

サーバに設置

プログラムを置きたい場所に移動し, git clone コマンドを実行する.
リポジトリへのパスは git@github.com の部分(コロンの左側)を .ssh/config に記述した HostName に変更する.

mnu@server:~/tmp$ git clone tutorial.github.com:usa-mimi/tutorial.git
Cloning into 'tutorial'...
remote: Counting objects: 491, done.
remote: Total 491 (delta 0), reused 0 (delta 0), pack-reused 491
Receiving objects: 100% (491/491), 325.51 KiB | 345.00 KiB/s, done.
Resolving deltas: 100% (255/255), done.
Checking connectivity... done.

gitコマンド

サーバ側で使う

リモートブランチの確認

git branch --remote もしくは
git branch -a

mnu@server:~/tmp/tutorial$ git branch --remote
  origin/HEAD -> origin/master
  origin/develop
  origin/master

ブランチの切り替え

git checkout ブランチ名

上記で確認したブランチへ切り替えるコマンド.
origin/ 部分は不要.

mnu@server:~/tmp/tutorial$ git checkout develop
Switched to branch 'develop'
Your branch is up-to-date with 'origin/develop'.

差分確認(内容確認)

git diff
pullした後でファイルを変更していないか確認するコマンド

mnu@server:~/tmp/tutorial$ git diff

差分がある場合は変更点が出る.

mnu@server:~/tmp/tutorial$ git diff
diff --git a/LICENSE b/LICENSE
index b1314a4..5ba7f18 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,5 @@
 The MIT License (MIT)
+oooo

 Copyright (c) 2015 MNU

gitの管理対象外のファイルは変更があっても出力されない

mnu@server:~/tmp/tutorial$ echo 'hogehoge' > aaa
mnu@server:~/tmp/tutorial$ git diff
diff --git a/LICENSE b/LICENSE
index b1314a4..5ba7f18 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,5 @@
 The MIT License (MIT)
+oooo

 Copyright (c) 2015 MNU

差分確認(ファイル確認)

git status

mnu@server:~/tmp/tutorial$ git status
On branch develop
Your branch is up-to-date with 'origin/develop'.
Changes not staged for commit:  # <-------------- 管理対象の差分が出る
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   LICENSE # <--- 変更したファイル

Untracked files:  # <--- 管理対象外の差分が出る
  (use "git add <file>..." to include in what will be committed)

    aaa # <--- 新しく追加されたファイル

no changes added to commit (use "git add" and/or "git commit -a")

変更の削除(管理対象ファイルの変更取り消し)

git checkout -f

mnu@server:~/tmp/tutorial$ git checkout -f
Your branch is up-to-date with 'origin/develop'.
mnu@server:~/tmp/tutorial$ git status
On branch develop
Your branch is up-to-date with 'origin/develop'.
# <--- 変更されたファイル「LICENSE」が消えている --->
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    aaa

nothing added to commit but untracked files present (use "git add" to track)

変更の削除(管理対象外のファイルの削除

削除するファイルの確認: git clean -n
削除の実行: git clean -f

mnu@server:~/tmp/tutorial$ git clean -n
Would remove aaa

mnu@server:~/tmp/tutorial$ git clean -f
Removing aaa

mnu@server:~/tmp/tutorial$ git status
On branch develop
Your branch is up-to-date with 'origin/develop'.
nothing to commit, working directory clean
6
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
6
9