GitHub に今まで書いたコードまとめておけば、再利用するときに楽ができるかしら、と思いました。
今まで GitHub を触ったことがありませんでしたので、今回使用してみた流れを残しておきますの♪
やってみること
Packer で Vagrant 用の最低限の OS Box を作成するコードを幾つか書いてまいりました。これを GitHub へホスティングしようと思います。
今回は、CentOS 5 の Box 化コードをホスティングした時の手順を残しますわ♪
ポイント
- GitHub はリモートリポジトリと同じように使用できる。サイトに様々な機能があるが、ひとまず無視して良い。
- コラボレーションしないならば、ただのリモートリポジトリとして使うのが楽。
GitHub を使う時の流れ
- GitHub サイトでリポジトリを作成
- ローカルに GitHub リモートリポジトリを clone
- ローカルで作業し、完成など一区切り付ける。
- GitHub リモートリポジトリへ push
最初に GitHub でリポジトリを作成し、次にそれをローカルに持ってくる、というのがポイントでしたの。
逆の、ローカルで作成したリポジトリを GitHub へアップロードする、というのは、実はできるのかもしれませんけれどもわかりませんでしたわ。
1. GitHub でリポジトリを作成
- アカウントを作成
- リポジトリを作成: oki2a24/packer-templates
2. GitHub リモートリポジトリの URL を取得
git clone xxxxx するための、xxxxx 部分を取得しました。
- oki2a24/packer-templates ページの [Clone or download] ボタン > [Copy to clipboard] で URL をコピー
3. ローカルに GitHub リモートリポジトリを clone
$ # 作成したリモートリポジトリをクローン
$ git clone https://github.com/oki2a24/packer-templates.git
Cloning into 'packer-templates'...
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (5/5), done.
Checking connectivity... done.
$
$ # clone できたかどうかディレクトリとフォルダ確認
$ ls -l
total 8
... 略 ...
drwxr-xr-x 6 oki2a24 staff 204 6 11 14:55 packer-templates
... 略 ...
$ cd packer-templates/
$ ls -al
total 40
drwxr-xr-x 6 oki2a24 staff 204 6 11 14:55 .
drwxr-xr-x+ 43 oki2a24 staff 1462 6 11 14:55 ..
drwxr-xr-x 12 oki2a24 staff 408 6 11 14:55 .git
-rw-r--r-- 1 oki2a24 staff 55 6 11 14:55 .gitignore
-rw-r--r-- 1 oki2a24 staff 11357 6 11 14:55 LICENSE
-rw-r--r-- 1 oki2a24 staff 18 6 11 14:55 README.md
$
$ # clone できたかどうか git log を確認
$ git log --all --decorate --graph --oneline
* bbb2698 (HEAD -> master, origin/master, origin/HEAD) Initial commit
$
$ # リモートリポジトリを確認
$ git remote show
origin
$ git remote show origin
* remote origin
Fetch URL: https://github.com/oki2a24/packer-templates.git
Push URL: https://github.com/oki2a24/packer-templates.git
HEAD branch: master
Remote branch:
master tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
$
4. ローカルリポジトリにてブランチを作成して作業
$ git checkout -b centos-5-x86_64 Switched to a new branch 'centos-5-x86_64' $ git log --all --decorate --graph --oneline * bbb2698 (HEAD -> centos-5-x86_64, origin/master, origin/HEAD, master) Initial commit $
作業終了!GitHub リモートリポジトリへ push
ローカルリポジトリでは作業完了、コミット済みの状態です。
$ # push 前の状態 $ git log --all --decorate --graph --oneline * ea402ea (HEAD -> centos-5-x86_64) entos-5.11-x86_64のVagrantBox完成 * bbb2698 (origin/master, origin/HEAD, master) Initial commit $ git remote show origin $ $ # push $ git push origin centos-5-x86_64 Username for 'https://github.com': username Password for 'https://username@github.com': Counting objects: 7, done. Delta compression using up to 4 threads. Compressing objects: 100% (7/7), done. Writing objects: 100% (7/7), 2.38 KiB | 0 bytes/s, done. Total 7 (delta 0), reused 0 (delta 0) To https://github.com/oki2a24/packer-templates.git * [new branch] centos-5-x86_64 -> centos-5-x86_64 $
5. GitHub リモートリポジトリ上でローカルリポジトリの変更をマージ
ローカルリポジトリで master へマージ後、リモートリポジトリへ push して反映する方法の方が楽かと思います。
けれども、せっかくなので GitHub サイト上で master ブランチへ merge しました♪
- Compare & pull request

- タイトル(コミットコメントがタイトルになる)、詳細を記入し、差分を確認して [Create pull request]
- This branch has no conflicts with the base branch と、競合なしにマージできることがわかる。他にも [Files changed] タブで違いを確認する。コメント部分で議論ができるのであろう。取り込みに問題がないため、[Merge pull request]

- マージ時にコミットを作りたいので、デフォルトの Create a merge commit のままにして、[Confirm merge]
![デフォルトの Create a merge commit のままにして、[Confirm merge]](https://i0.wp.com/oki2a24.com/wp-content/uploads/2016/06/my-first-github-3.jpg?resize=580%2C507&ssl=1)
- マージが成功したので、ブランチを削除する。[Delete branch]
![マージが成功したので、ブランチを削除する。[Delete branch]](https://i0.wp.com/oki2a24.com/wp-content/uploads/2016/06/my-first-github-4.jpg?resize=580%2C507&ssl=1)
これで、いつでもホスティングしたコードを利用できるようになりました♪
6. ローカルリポジトリに反映する
GitHub リモートリポジトリ上でマージしたので、その変更をローカルリポジトリへと反映し、次の開発を始められるようにします。
- ローカルブランチを削除
- リモートリポジトリを fetch
という手順が一般的と存じますが、あえて逆でやってみます。
つまり、リモートリポジトリの変更をまず取り込んでから、不要なブランチを削除して整理してみたいですの♪
$ git fetch origin remote: Counting objects: 1, done. remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (1/1), done. From https://github.com/oki2a24/packer-templates bbb2698..4878f7f master -> origin/master $ git log --all --decorate --graph --oneline * 4878f7f (origin/master, origin/HEAD) Merge pull request #1 from oki2a24/centos-5-x86_64 |\ | * ea402ea (HEAD -> centos-5-x86_64, origin/centos-5-x86_64) entos-5.11-x86_64のVagrantBox完成 |/ * bbb2698 (master) Initial commit $
7. ローカルブランチを削除
$ git checkout master Switched to branch 'master' Your branch is behind 'origin/master' by 2 commits, and can be fast-forwarded. (use "git pull" to update your local branch) $ git branch -D centos-5-x86_64 Deleted branch centos-5-x86_64 (was ea402ea). $ git log --all --decorate --graph --oneline * 4878f7f (origin/master, origin/HEAD) Merge pull request #1 from oki2a24/centos-5-x86_64 |\ | * ea402ea (origin/centos-5-x86_64) entos-5.11-x86_64のVagrantBox完成 |/ * bbb2698 (HEAD -> master) Initial commit $
8. すでに無いはずのリモートブランチ参照を削除する
GitHub 上で削除したブランチ origin/centos-5-x86_64 が、ローカルリポジトリには残っています。
これを削除したかったのですけれども、ダメでした><。
リモートブランチとの追跡関係も(当然ですけれども)切れておりますし、放っておきますの。
$ # サーバのリモートブランチを削除しようとするも、ダメ $ git push origin --delete centos-5-x86_64 error: unable to delete 'centos-5-x86_64': remote ref does not exist error: failed to push some refs to 'https://github.com/oki2a24/packer-templates.git' $ # ローカルの参照だけ削除できるのだろうか?とやってみたが、ダメ git branch -D origin/centos-5-x86_64 error: branch 'origin/centos-5-x86_64' not found. $ $ # どのリモートブランチを追跡しているか確認 $ git branch -vv * master bbb2698 [origin/master: behind 2] Initial commit $
9. ローカルの master ブランチの状態を、GitHub リモートブランチと同じ状態にする。
マージいたします。
$ # 取り込み先のブランチにいることを確認 $ git branch * master $ $ # マージ $ git merge origin/master Updating bbb2698..4878f7f Fast-forward .gitignore | 3 +++ centos-5.11-x86_64.json | 34 ++++++++++++++++++++++++++++++++++ centos-5.11-x86_64/anaconda-ks.cfg | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ centos-5.11-x86_64/scripts.sh | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+) create mode 100644 centos-5.11-x86_64.json create mode 100644 centos-5.11-x86_64/anaconda-ks.cfg create mode 100644 centos-5.11-x86_64/scripts.sh $
これで、次の開発を始められますの♪つぎは、master へマージ済みのものを push してみようかしら。
おわりに
git コマンドをよく忘れてしまいますの><。今回は次のページに助けられましたわ♪ありがとう存じます!
以上です。
