LoginSignup
15
20

More than 5 years have passed since last update.

GitHubユーザアカウントとGitHubリポジトリ作成 + AWS EC2からGitHubリポジトリへpushする手順例

Last updated at Posted at 2016-09-22

はじめに

GitHubユーザアカウントとGitHubリポジトリ作成、AWS EC2からGitHubリポジトリへpushする手順の一例を記載致します。

環境

今回の手順で使用したAWS EC2インスタンス(Amazon Linux)は以下のAMIで作成しました。

・Amazon Linux AMI 2016.03.3 (HVM), SSD Volume Type - ami-374db956
  amzn-ami-hvm-2016.03.3.x86_64-gp2 (ami-374db956)

EC2インスタンスにはyumでgitをインストールしました。バージョンは以下になります。
・git version 2.7.4

参考資料

以下の手順を参考にさせて頂きました。ありがとうございました。

Githubに新規リポジトリ(Repository)を作成する
http://qiita.com/bakainubau/items/4613dda50a5fa302d212

GitHubアカウント作成とリポジトリの作成手順
http://qiita.com/kooohei/items/361da3c9dbb6e0c7946b

GitHubで二段階認証に変更する
http://qiita.com/katsukii/items/9773e9cfea7d929fcf9c

git パスワード を毎回聞かれる問題の解決方法
http://qiita.com/rorensu2236/items/df7d4c2cf621eeddd468

Gitの最初のコミットは空コミットにしよう
http://qiita.com/NorsteinBekkler/items/b2418cd5e14a52189d19

gitコマンドの基本的な使い方
http://qiita.com/wwacky/items/2f110ee76fc1cb681c3b

GitHubの初期設定(SSH接続からリポジトリへのpushまで)
http://qiita.com/drapon/items/441e18452b25060d61f1

githubで2段階認証を設定したらpushできなくなった
http://qiita.com/yoan/items/08b1ba9c9aceab1ffe5e

GitHubユーザアカウントとGitHubリポジトリ作成手順

(1) GitHub( https://github.com/ )へ接続します。

(2) GitHubユーザアカウントを作成します。

以下のページを参考にして、GitHubにユーザアカウントを作成します。

「Sign up for GitHub」をクリックして、GitHubユーザアカウントを作成します。本手順では例として以下のユーザアカウントを作成します。

設定 設定例
作成するGitHubユーザアカウント名 exampleuser
作成するGitHubユーザアカウントのメールアドレス example.user@example.com
GitHubユーザアカウントのURL https://github.com/example_user
作成するGitHubリポジトリ名 example-repositories
作成するGitHubリポジトリのURL https://github.com/exampleuser/example-repositories.git

(3) AWSにEC2インスタンスを作成します。

以下のページを参考にして、AWSにEC2インスタンスを作成します。
今回の例ではAmazon LinuxのAMIを使用して、EC2インスタンスを作成します。

(4) EC2インスタンスにsshログインして、gitをインストールします。

ssh -i [EC2インスタンスログイン用のKey Pairファイル名] ec2-user@EC2インスタンスのIPアドレス
[ec2-user@git-client ~]$ sudo yum -y update
[ec2-user@git-client ~]$ sudo yum -y install git
[ec2-user@git-client ~]$ git --version
git version 2.7.4
[ec2-user@git-client ~]$

(5) GitHub接続用のSSH公開鍵と秘密鍵を作成します。

環境変数にGitHubユーザアカウント名とEメールアドレスをセットします。

[ec2-user@git-client ~]$ export GIT_EMAIL="example.user@example.com"
[ec2-user@git-client ~]$ export GIT_USER="exampleuser"
[ec2-user@git-client ~]$

作成するGitHubユーザアカウント用のSSH公開鍵ファイル名をセットします。

[ec2-user@git-client ~]$ export SSH_KEY_NAME="/home/ec2-user/.ssh/id_rsa.GitHub_$GIT_USER"
[ec2-user@git-client ~]$

GitHubユーザアカウント用のSSH公開鍵と秘密鍵を作成します。
ここで作成した公開鍵は後でGitHubに登録します。詳細は後述します。

[ec2-user@git-client ~]$ ssh-keygen -t rsa -C "$EMAIL" -f $SSH_KEY_NAME
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): -> パスフレーズは入力せずにEnterキーを押下します。
Enter same passphrase again: -> パスフレーズは入力せずにEnterキーを押下します。
Your identification has been saved in /home/ec2-user/.ssh/id_rsa.GitHub_exampleuser.
Your public key has been saved in /home/ec2-user/.ssh/id_rsa.GitHub_exampleuser.pub.
The key fingerprint is:
XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX example.user@example.com
The key's randomart image is:
+--[ RSA 2048]----+
|        XXXXX    |
|         XX  X   |
|      X XXX   X  |
|     X X X X X   |
|      X X X X    |
|     X X X X     |
|    X            |
|                 |
|                 |
+-----------------+
[ec2-user@git-client ~]$

(6) SSH公開鍵と秘密鍵のパーミッションを変更します。

作成したGitHubユーザアカウント用のSSH公開鍵と秘密鍵のパーミッションを変更します。

[ec2-user@git-client ~]$ chmod 644 $SSH_KEY_NAME.pub
[ec2-user@git-client ~]$ chmod 600 $SSH_KEY_NAME
[ec2-user@git-client ~]$

SSH公開鍵と秘密鍵のパーミッションが以下になっている事を確認します。

公開鍵ファイルは(id_rsa.GitHub_exampleuser.pub)は「-rw-r--r--」、
秘密鍵ファイルは(id_rsa.GitHub_exampleuser)「-rw-------」になっている事を確認します。

[ec2-user@git-client ~]$ ls -lrta $SSH_KEY_NAME*
-rw-r--r-- 1 ec2-user ec2-user  404 Sep 20 23:57 /home/ec2-user/.ssh/id_rsa.GitHub_exampleuser.pub
-rw------- 1 ec2-user ec2-user 1679 Sep 20 23:57 /home/ec2-user/.ssh/id_rsa.GitHub_exampleuser
[ec2-user@git-client ~]$

(7) https://github.com/ に作成したGitHubユーザアカウントでログインします。

https://github.com/ へ接続して「Sign in」をクリックし、作成したGitHubユーザアカウントでログインします。

GitHubログイン後、画面右上の「Settings」をクリックします。

「SSH and GPG keys」をクリックします。

https://github.com/settings/keys
「SSH Keys」->「New SSH Key」をクリックします。

「Title」に以下を入力します。

/home/ec2-user/.ssh/id_rsa.GitHub_example_user.pub`

「Key」に前述(3)の手順で作成した公開鍵ファイル(/home/ec2-user/.ssh/id_rsa.GitHub_exampleuser.pub)の内容を入力します。

ssh-rsa AAAAB*****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************3L example.user@example.com

「Add SSH Key」をクリックし、公開鍵をGitHubに登録します。

(8) GitHubユーザアカウントの2段階認証を設定します。

以下のページを参考にして、GitHubユーザアカウントログインに2段階認証を設定します。

GitHubログイン後、画面右上の「Settings」をクリックします。
https://github.com/settings/security

「Two-factor authentication」の「Set up two-factor authentication」をクリックして、GitHubの2段階認証を設定します。

(9) EC2インスタンスにGitHub接続用の.ssh/configを作成します。

[ec2-user@git-client ~]$ vi /home/ec2-user/.ssh/config
Host github.com
  HostName github.com
  IdentityFile /home/ec2-user/.ssh/id_rsa.GitHub_example_user
  User git

.ssh/configのパーミッションは600にする必要があるので、パーミッションを変更します。

[ec2-user@git-client ~]$ chmod 600 /home/ec2-user/.ssh/config
[ec2-user@git-client ~]$ ll /home/ec2-user/.ssh/config
-rw------- 1 ec2-user ec2-user 107 Sep 21 00:04 /home/ec2-user/.ssh/config
[ec2-user@git-client ~]$

(10) EC2インスタンスからGitHubへ接続出来るかテストします。

GitHubへの初回接続時「Are you sure you want to continue connecting」と表示されるので「yes」と入力します。

[ec2-user@git-client ~]$ ssh -T github.com
The authenticity of host 'github.com (XXX.XXX.XXX.XXX)' can't be established.
RSA key fingerprint is XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX.
Are you sure you want to continue connecting (yes/no)? yes -> yesと入力します。
Warning: Permanently added 'github.com,XXX.XXX.XXX.XXX' (RSA) to the list of known hosts.
Hi example_user! You've successfully authenticated, but GitHub does not provide shell access.
[ec2-user@git-client ~]$

以下のように表示されていれば、EC2インスタンスからGitHubへの接続は正常です。

[ec2-user@git-client ~]$ ssh -T github.com
Hi example_user! You've successfully authenticated, but GitHub does not provide shell access.
[ec2-user@git-client ~]$

(11) GitHubユーザアカウント名とメールアドレスを設定します。

GitHubユーザアカウント名とメールアドレスを設定します。

[ec2-user@git-client ~]$ pwd
/home/ec2-user

[ec2-user@git-client ~]$ git config --global user.name "exampleuser"
[ec2-user@git-client ~]$ git config --global user.email "example.user@example.com"
[ec2-user@git-client ~]$

出力にカラーをつけたい場合は以下も実行します(任意)。

[ec2-user@git-client ~]$ git config --global color.ui auto
[ec2-user@git-client ~]$

.gitconfigが作成されます。

[ec2-user@git-client ~]$ cat /home/ec2-user/.gitconfig
[user]
        name = exampleuser
        email = example.user@example.com
[color]
        ui = auto
[ec2-user@git-client ~]$

(12) https://github.com/ にログインしてGitHubリポジトリを作成します。

https://github.com/ へ接続して「Sign in」をクリックし、作成したGitHubユーザアカウントでログインします。

GitHubログイン後、画面右上の「Your profile」をクリックします。

「Repositories」をクリックします。
https://github.com/exampleuser?tab=repositories

「New」をクリックします。

GitHubリポジトリ作成画面が表示されます。
https://github.com/new

以下のように作成するリポジトリ名等を入力します。

設定 入力例 備考
Owner exampleuser
Repository name example-repositories
Description (optional) example repositories
リポジトリのタイプ Public Publicにするとリポジトリは誰でも閲覧可能になります。
Initialize this repository with a README チェックをつける リポジトリ作成時、README.mdが一緒に作成されます。

「Create Repository」をクリックし、GitHubリポジトリを作成します。

(13) GitHubリポジトリが作成された事を確認します。

前述の手順でGitHubリポジトリを作成すると、作成したGitHubリポジトリが表示されます。

画面右側にある「Clone or download」をクリックします。

「Clone with HTTPS」にGitHubリポジトリをgit cloneする時のURLが表示されるので、このURLを確認します。

https://github.com/exampleuser/example-repositories.git

(14) 作成したGitHubリポジトリをgit cloneコマンドでダウンロードします。

EC2インスタンスでgit cloneコマンドを実行し、作成したGitHubリポジトリをダウンロードします。

リポジトリをダウンロードしたいディレクトリへ移動します。

[ec2-user@git-client ~]$ cd /home/ec2-user

作成したGitHubリポジトリをgit cloneします。

[ec2-user@git-client ~]$ pwd
/home/ec2-user
[ec2-user@git-client ~]$ git clone https://github.com/exampleuser/example-repositories.git
Cloning into 'example-repositories'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
Checking connectivity... done.
[ec2-user@git-client ~]$
[ec2-user@git-client ~]$ ls -lrta /home/ec2-user/example-repositories/
total 16
drwx------ 7 ec2-user ec2-user 4096 Sep 22 19:18 ..
-rw-rw-r-- 1 ec2-user ec2-user   44 Sep 22 19:18 README.md
drwxrwxr-x 8 ec2-user ec2-user 4096 Sep 22 19:18 .git
drwxrwxr-x 3 ec2-user ec2-user 4096 Sep 22 19:18 .
[ec2-user@git-client ~]$

(15) git remote set-urlコマンドでリモートリポジトリoriginのURLを指定します。

git remote set-urlコマンドを実行し、GitHubリモートリポジトリoriginのURLを指定します。また、GitHubユーザアカウントの2段階認証設定した場合、push時に毎回パスワード入力を要求されるのを省略する為、URLをhttpsからgitプロトコルへ変更します。

まずgit cloneによりリポジトリをダウンロードしたディレクトリへ移動します。

[ec2-user@git-client ~]$ cd /home/ec2-user/example-repositories/
[ec2-user@git-client example-repositories]$
[ec2-user@git-client example-repositories]$ pwd
/home/ec2-user/example-repositories
[ec2-user@git-client example-repositories]$

現状のoriginのURLを確認します。

[ec2-user@git-client example-repositories]$ git remote -v
origin  https://github.com/exampleuser/example-repositories.git (fetch)
origin  https://github.com/exampleuser/example-repositories.git (push)
[ec2-user@git-client example-repositories]$

リモートリポジトリoriginのURL指定は以下のような形式で指定します。

git@github.com:GitHubユーザアカウント名/GitHubリポジトリ名.git

なお「git@github.com~」の部分は固定です。「exampleuser@github.com~」のように変更する必要はありません。

今回の例の場合、以下のように指定します。

git@github.com:exampleuser/example-repositories.git

git remote set-urlコマンドを実行し、リモートリポジトリoriginのURLを指定します。

[ec2-user@git-client example-repositories]$ git remote set-url origin git@github.com:exampleuser/example-repositories.git
[ec2-user@git-client example-repositories]$

originのURLが変更された事を確認します。

[ec2-user@git-client example-repositories]$ git remote -v
origin  git@github.com:exampleuser/example-repositories.git (fetch)
origin  git@github.com:exampleuser/example-repositories.git (push)
[ec2-user@git-client example-repositories]$

(16) GitHubリポジトリにファイルを追加してpushします。

以下の例ではトピックブランチは作成せず、ローカルmasterブランチにファイルを追加し、GitHubリポジトリのリモートmasterブランチへpushする流れの手順を記します。

備考


実際の開発、複数人で開発する場合は、リポジトリのローカルのmasterブランチへいきなりファイルを追加したり変更するのではなく、以下のような流れになる事が多いかと思います。

 ・ローカルにトピックブランチを作成する。
   ↓
 ・ローカルのトピックブランチに対して、ファイルを作成したり、変更を加える。
   ↓
 ・ローカルのトピックブランチから、リモートのトピックブランチへ変更をpushする。
   ↓
 ・プルリクエストを作成する。
   ↓
 ・トピックブランチに加えた変更について、コードレビューをレビュアーへ依頼する。
   ↓
 ・コードレビューで問題なければ、masterブランチにトピックブランチの変更をマージしてもらう。

※トピックブランチを作り、プルリクエストでmasterブランチへ変更を加える手順例については、本ページ末尾の
「GitHubリポジトリにトピックブランチを作成してプルリクエストを使って、masterブランチへ変更を加える手順」
をご参照下さい。


masterブランチへpushする手順について記します。
git cloneでダウンロードしたリポジトリへ移動します。

[ec2-user@git-client example-repositories]$ pwd
/home/ec2-user/example-repositories
[ec2-user@git-client example-repositories]$ git branch
* master
[ec2-user@git-client example-repositories]$

GitHubリポジトリへ追加したいファイルを作成します。
今回の例ではローカルmasterブランチに対して、ファイルを作成します。

[ec2-user@git-client example-repositories]$ echo 'echo "hello world"' > example.sh
[ec2-user@git-client example-repositories]$
[ec2-user@git-client example-repositories]$ chmod 755 example.sh
[ec2-user@git-client example-repositories]$
[ec2-user@git-client example-repositories]$ git status -s
?? example.sh
[ec2-user@git-client example-repositories]$

作成したファイルをGitのインデックスに追加します。

[ec2-user@git-client example-repositories]$ git add example.sh
[ec2-user@git-client example-repositories]$

作成したファイルをcommitします。

[ec2-user@git-client example-repositories]$ git status -s
A  example.sh
[ec2-user@git-client example-repositories]$
[ec2-user@git-client example-repositories]$ git commit -m "example commit"
[master ebc0119] example commit
 1 file changed, 1 insertion(+)
 create mode 100755 example.sh
[ec2-user@git-client example-repositories]$
[ec2-user@git-client example-repositories]$ git status -s
[ec2-user@git-client example-repositories]$

GitHubリポジトリのリモートmasterブランチへpushします。

[ec2-user@git-client example-repositories]$ git push origin master
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 305 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:exampleuser/example-repositories.git
   d9f41c8..ebc0119  master -> master
[ec2-user@git-client example-repositories]$
[ec2-user@git-client example-repositories]$ git branch
* master
[ec2-user@git-client example-repositories]$

(17) GitHubリポジトリにファイルが追加された事を確認します。

方法1 ブラウザからgithub.comへ接続して確認する。

ブラウザからGitHubへ接続して、GitHubリポジトリにファイルが追加された事を確認します。

example.sh
echo "hello world"

方法2 トピックブランチを作成して確認する。

試しにトピックブランチ「examplebranch」を作成します。

[ec2-user@git-client example-repositories]$ pwd
/home/ec2-user/example-repositories
[ec2-user@git-client example-repositories]$ git branch
* master
[ec2-user@git-client example-repositories]$
[ec2-user@git-client example-repositories]$ git checkout -b examplebranch
Switched to a new branch 'examplebranch'
[ec2-user@git-client example-repositories]$

ローカルに作成したトピックブランチとリモートmasterブランチに差異がない事を確認します。

[ec2-user@git-client example-repositories]$ git diff origin/master
[ec2-user@git-client example-repositories]$
[ec2-user@git-client example-repositories]$ git branch
* examplebranch
  master
[ec2-user@git-client example-repositories]$
[ec2-user@git-client example-repositories]$ ls -lrta /home/ec2-user/example-repositories
total 20
-rw-rw-r-- 1 ec2-user ec2-user   44 Sep 22 19:18 README.md
-rwxr-xr-x 1 ec2-user ec2-user   19 Sep 22 19:44 example.sh
drwxrwxr-x 3 ec2-user ec2-user 4096 Sep 22 20:23 .
drwx------ 7 ec2-user ec2-user 4096 Sep 22 20:23 ..
drwxrwxr-x 8 ec2-user ec2-user 4096 Sep 22 20:26 .git
[ec2-user@git-client example-repositories]$
[ec2-user@git-client example-repositories]$ cat /home/ec2-user/example-repositories/example.sh
echo "hello world"
[ec2-user@git-client example-repositories]$

方法3 GitHubリポジトリのリモートmasterブランチから最新のファイルをpullして確認する

GitHubリポジトリのリモートmasterブランチから最新のファイルをpullして、ファイルが追加された事を確認します。

[ec2-user@git-client example-repositories]$ pwd
/home/ec2-user/example-repositories
[ec2-user@git-client example-repositories]$ git branch
* master
[ec2-user@git-client example-repositories]$ git pull origin master
From github.com:exampleuser/example-repositories
 * branch            master     -> FETCH_HEAD
Already up-to-date.
[ec2-user@git-client example-repositories]$
[ec2-user@git-client example-repositories]$ ls -lrta /home/ec2-user/example-repositories/
total 20
drwx------ 7 ec2-user ec2-user 4096 Sep 22 19:18 ..
-rw-rw-r-- 1 ec2-user ec2-user   44 Sep 22 19:18 README.md
drwxrwxr-x 3 ec2-user ec2-user 4096 Sep 22 19:44 .
-rwxr-xr-x 1 ec2-user ec2-user   19 Sep 22 19:44 example.sh
drwxrwxr-x 8 ec2-user ec2-user 4096 Sep 22 19:52 .git
[ec2-user@git-client example-repositories]$

ローカルとリモートmasterブランチに差異がない事を確認します。

[ec2-user@git-client example-repositories]$ git diff origin/master
[ec2-user@git-client example-repositories]$

先ほどGitHubリポジトリに追加したファイルがある事を確認します。

[ec2-user@git-client example-repositories]$ cat /home/ec2-user/example-repositories/example.sh
echo "hello world"
[ec2-user@git-client example-repositories]$

GitHubリポジトリにトピックブランチを作成してプルリクエストを使って、masterブランチへ変更を加える手順

GitHubリポジトリにトピックブランチを作成してプルリクエストを送って、masterブランチへピックブランチに加えた変更内容をマージする手順も合わせて記載致します。

(1) GitHubリポジトリを作成します。

例えば以下のGitHubリポジトリを作成します。

(2) 作成したGitHubリポジトリをgit cloneコマンドでダウンロードします。

[ec2-user@git-client ~]$ pwd
/home/ec2-user
[ec2-user@git-client ~]$ git clone https://github.com/na0AaooQ/crontab-to-csv.git
Cloning into 'crontab-to-csv'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
Checking connectivity... done.
[ec2-user@git-client ~]$

(3) 念の為、ダウンロードしたローカルmasterブランチとリモートmasterブランチに差異がない事を確認します。

[ec2-user@git-client ~]$ cd crontab-to-csv/
[ec2-user@git-client crontab-to-csv]$ pwd
/home/ec2-user/crontab-to-csv
[ec2-user@git-client crontab-to-csv]$ ls -lrta
total 16
drwx------ 12 ec2-user ec2-user 4096 Oct  2 22:41 ..
-rw-rw-r--  1 ec2-user ec2-user   78 Oct  2 22:41 README.md
drwxrwxr-x  8 ec2-user ec2-user 4096 Oct  2 22:41 .git
drwxrwxr-x  3 ec2-user ec2-user 4096 Oct  2 22:41 .
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ git branch
* master
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ git pull origin master
From https://github.com/na0AaooQ/crontab-to-csv
 * branch            master     -> FETCH_HEAD
Already up-to-date.
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ git diff origin/master
[ec2-user@git-client crontab-to-csv]$

(4) git remote set-urlコマンドでリモートリポジトリoriginのURLを指定します。

[ec2-user@git-client crontab-to-csv]$ git remote -v
origin  https://github.com/na0AaooQ/crontab-to-csv.git (fetch)
origin  https://github.com/na0AaooQ/crontab-to-csv.git (push)
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ git remote set-url origin git@github.com:na0AaooQ/crontab-to-csv.git
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ git remote -v
origin  git@github.com:na0AaooQ/crontab-to-csv.git (fetch)
origin  git@github.com:na0AaooQ/crontab-to-csv.git (push)
[ec2-user@git-client crontab-to-csv]$

(4) トピックブランチを作成します。

[ec2-user@git-client crontab-to-csv]$ pwd
/home/ec2-user/crontab-to-csv
[ec2-user@git-client crontab-to-csv]$ git branch
* master
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ git checkout -b add-script
Switched to a new branch 'add-script'
[ec2-user@git-client crontab-to-csv]$

(5) 作成したトピックブランチとリモートmasterブランチに差異がない事を確認します。

[ec2-user@git-client crontab-to-csv]$ git branch
* add-script
  master
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ git diff origin/master
[ec2-user@git-client crontab-to-csv]$

(6) 作成したトピックブランチにファイルを追加、ファイルを変更します。

[ec2-user@git-client crontab-to-csv]$ pwd
/home/ec2-user/crontab-to-csv
[ec2-user@git-client crontab-to-csv]$ git branch
* add-script
  master
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ git status -s
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ ls -lrta
total 16
drwx------ 12 ec2-user ec2-user 4096 Oct  2 22:41 ..
-rw-rw-r--  1 ec2-user ec2-user   78 Oct  2 22:41 README.md
drwxrwxr-x  3 ec2-user ec2-user 4096 Oct  2 22:41 .
drwxrwxr-x  8 ec2-user ec2-user 4096 Oct  2 22:50 .git
[ec2-user@git-client crontab-to-csv]$
crontab_to_csv.sh
[ec2-user@git-client crontab-to-csv]$ vi crontab_to_csv.sh
#!/bin/bash

crontab_csv_file="crontab.csv"

echo "cron_host,cron_user,minutes,hours,days,months,day_of_the_week,cron_name,cron_parameter" > $crontab_csv_file
crontab -l | grep -v ^# | grep -v ^$ | grep -v MAILTO | sed -e "s/  */ /g" -e "s/ /,/1" -e "s/ /,/1" -e "s/ /,/1" -e "s/ /,/1" -e "s/ /,/1" | awk '{$0="'`hostname -s`','$USER'," $0 "";print}' >> $crontab_csv_file

if [ -f $crontab_csv_file ] ; then
  cat $crontab_csv_file
else
  echo "file not found $crontab_csv_file."
fi
[ec2-user@git-client crontab-to-csv]$ chmod 755 crontab_to_csv.sh
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ sh -n crontab_to_csv.sh
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ cat crontab_to_csv.sh
#!/bin/bash

crontab_csv_file="crontab.csv"

echo "cron_host,cron_user,minutes,hours,days,months,day_of_the_week,cron_name,cron_parameter" > $crontab_csv_file
crontab -l | grep -v ^# | grep -v ^$ | grep -v MAILTO | sed -e "s/  */ /g" -e "s/ /,/1" -e "s/ /,/1" -e "s/ /,/1" -e "s/ /,/1" -e "s/ /,/1" | awk '{$0="'`hostname -s`','$USER'," $0 "";print}' >> $crontab_csv_file

if [ -f $crontab_csv_file ] ; then
  cat $crontab_csv_file
else
  echo "file not found $crontab_csv_file."
fi
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ vi README.md
# crontab-to-csv
LinuxのcrontabをCSVファイルで出力するシェルスクリプト

http://qiita.com/na0AaooQ/items/b944af792ac763f482ff

\```
git clone https://github.com/na0AaooQ/crontab-to-csv.git
\```
[ec2-user@git-client crontab-to-csv]$ pwd
/home/ec2-user/crontab-to-csv
[ec2-user@git-client crontab-to-csv]$ ls -lrta
total 20
-rwxr-xr-x  1 ec2-user ec2-user  483 Oct  2 22:51 crontab_to_csv.sh
-rw-rw-r--  1 ec2-user ec2-user  210 Oct  2 22:54 README.md
drwx------ 12 ec2-user ec2-user 4096 Oct  2 22:54 ..
drwxrwxr-x  8 ec2-user ec2-user 4096 Oct  2 22:55 .git
drwxrwxr-x  3 ec2-user ec2-user 4096 Oct  2 22:55 .
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ git status -s
 M README.md
?? crontab_to_csv.sh
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ git add README.md
[ec2-user@git-client crontab-to-csv]$ git add crontab_to_csv.sh
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ sh -n crontab_to_csv.sh
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ git diff origin/master
diff --git a/README.md b/README.md
index 3c79369..04144d9 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,8 @@
 # crontab-to-csv
-LinuxのcrontabをCSVで出力するシェルスクリプト
+LinuxのcrontabをCSVファイルで出力するシェルスクリプト
+
+http://qiita.com/na0AaooQ/items/b944af792ac763f482ff
+
+```
+git clone https://github.com/na0AaooQ/crontab-to-csv.git
+```
diff --git a/crontab_to_csv.sh b/crontab_to_csv.sh
new file mode 100755
index 0000000..6b62589
--- /dev/null
+++ b/crontab_to_csv.sh
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+crontab_csv_file="crontab.csv"
+
+echo "cron_host,cron_user,minutes,hours,days,months,day_of_the_week,cron_name,cron_parameter" > $crontab_csv_file
+crontab -l | grep -v ^# | grep -v ^$ | grep -v MAILTO | sed -e "s/  */ /g" -e "s/ /,/1" -e "s/ /,/1" -e "s/ /,/1" -e "s/ /,/1" -e "s/ /,/1" | awk '{$0="'`hostname -s`','$USER'," $0 "";print}' >> $crontab_csv_file
+
+if [ -f $crontab_csv_file ] ; then
+  cat $crontab_csv_file
+else
+  echo "file not found $crontab_csv_file."
+fi
[ec2-user@git-client crontab-to-csv]$

(7) トピックブランチに追加・変更したファイルをcommitします。

[ec2-user@git-client crontab-to-csv]$ pwd
/home/ec2-user/crontab-to-csv
[ec2-user@git-client crontab-to-csv]$ git branch
* add-script
  master
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ git commit -m "add script and modify readme"
[add-script 15d87ef] add script and modify readme
 2 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100755 crontab_to_csv.sh
[ec2-user@git-client crontab-to-csv]$

(7) ローカルトピックブランチをリモートトピックブランチへpushします。

[ec2-user@git-client crontab-to-csv]$ pwd
/home/ec2-user/crontab-to-csv
[ec2-user@git-client crontab-to-csv]$ git branch
* add-script
  master
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ git push origin add-script
Counting objects: 4, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 740 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To git@github.com:na0AaooQ/crontab-to-csv.git
 * [new branch]      add-script -> add-script
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ git branch -a
* add-script
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/add-script
  remotes/origin/master
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ git diff origin/add-script
[ec2-user@git-client crontab-to-csv]$

(8) リモートトピックブランチへpushした変更内容を元に、プルリクエストを作成します。

GitHubリポジトリの画面を表示します。

先ほどのpushにより「Your recently pushed branches:」と表示されるので「Compare & pull request」をクリックします。

プルリクエストのタイトルやコメント欄に、コードの変更内容やソースレビュー依頼内容を記入して、「Create pull request」をクリックします。

(9) トピックブランチへpushした変更内容に問題なければ、プルリクエストの変更内容をmasterブランチへマージします。

トピックブランチへpushした変更内容に問題なければ「Merge pull request」をクリックして、トピックブランチの変更内容をmasterブランチへマージします。

「Confirm merge」をクリックします。

(10) トピックブランチプルリクエストのmasterマージ後、リモートのトピックブランチを削除します。

「Delete branch」をクリックします。

(11) GitHubリポジトリのmasterブランチにプルリクエストの変更が反映された事を確認します。

[ec2-user@git-client crontab-to-csv]$ pwd
/home/ec2-user/crontab-to-csv
[ec2-user@git-client crontab-to-csv]$ git branch
* add-script
  master
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ git branch
  add-script
* master
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ ls -lrta
total 16
drwx------ 12 ec2-user ec2-user 4096 Oct  2 22:54 ..
-rw-rw-r--  1 ec2-user ec2-user   78 Oct  2 23:12 README.md
drwxrwxr-x  8 ec2-user ec2-user 4096 Oct  2 23:12 .git
drwxrwxr-x  3 ec2-user ec2-user 4096 Oct  2 23:12 .
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ git pull origin master
remote: Counting objects: 1, done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (1/1), done.
From github.com:na0AaooQ/crontab-to-csv
 * branch            master     -> FETCH_HEAD
   7d4fe61..13e8935  master     -> origin/master
Updating 7d4fe61..13e8935
Fast-forward
 README.md         |  8 +++++++-
 crontab_to_csv.sh | 12 ++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100755 crontab_to_csv.sh
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ pwd
/home/ec2-user/crontab-to-csv
[ec2-user@git-client crontab-to-csv]$ ls -lrta
total 20
drwx------ 12 ec2-user ec2-user 4096 Oct  2 22:54 ..
-rw-rw-r--  1 ec2-user ec2-user  210 Oct  2 23:13 README.md
-rwxrwxr-x  1 ec2-user ec2-user  483 Oct  2 23:13 crontab_to_csv.sh
drwxrwxr-x  3 ec2-user ec2-user 4096 Oct  2 23:13 .
drwxrwxr-x  8 ec2-user ec2-user 4096 Oct  2 23:13 .git
[ec2-user@git-client crontab-to-csv]$
[ec2-user@git-client crontab-to-csv]$ git branch
  add-script
* master
[ec2-user@git-client crontab-to-csv]$ git diff origin/master
[ec2-user@git-client crontab-to-csv]$

以上になります。

15
20
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
15
20