Git基礎: リモートリポジトリ

リモートリポジトリを使う手順
1. 空のリモートリポジトリを作る
2. ローカルリポジトリの内容をリモートリポジトリに送る
3. ローカルリポジトリで作業して内容を更新する

2, 3を繰り返す

### リモートリポジトリを上流リポジトリとする
$ git init
$ git remote add origin https://github.com/hpscript/git101.git
$ git remote -v

$ git push origin master

### リモート追跡ブランチ
$ git branch -r
$ git branch -r -v

### クローン
$ git clone https://github.com/hpscript/git101.git

### リモートの更新を取得
$ git fetch リモート リモートブランチ

### リモートの更新をローカルに反映
git checkout ローカルブランチ
git merge –ff-only リモート追跡ブランチ
$ git checkout master
$ git merge –ff-only origin/master

### リモートブランチとローカルブランチを対応づけ
git branch -u
git branch -u origin/master

うーん、なんだかなー

Git基礎: タグ

Gitのタグの種類
– 軽量タグ
– 注釈付きタグ
– 署名タグ

### 軽量タグをつける
git tag タグ名
git tag タグ名 コミット

$ git tag v1.0
$ git tag
v1.0

prog.sh

#!/bin/sh

echo "Your age?"; read age
echo "Your age is $age."

$ git add .
$ git commit -m “first commit”
$ git tag v1.0
$ git tag
$ git show

### 注釈付きタグ
git tag -a タグ名
git tag -a タグ名 コミット

$ git add -u
$ git tag -a v3.0 -m “version3.0”
$ git show

### タグのついたコミットをチェックアウトする
git checkout タグ
$ git tag v1.1
$ git branch
master
* v1fix

### タグを削除する
git tag -d タグ
$ git tag -d v2.0

ニャルほどー

git基礎: ブランチ

ブランチとは履歴の枝分かれ
変更をmasterブランチに取り込むことをマージ

program.txt

春の大運動会
* 開会式
* 閉会式

$ git add program.txt
$ git commit -m “プログラム作成開始”

### ブランチを作る
git branch ブランチ名
$ git add -u
※バージョン管理されていて、変更があったすべてのファイルがaddされる
$ git commit -m “玉関係の競技追加”
$ git branch proc
$ git branch
$ git branch -v
* master 69c78cb 玉関係の競技追加
proc 69c78cb 玉関係の競技追加
git branchに-vオプションを与えると、ブランチの先頭があるコミット情報も表示される

$ git add -u
$ git commit -m “走る競技追加”
$ git branch -v
* master 66006b7 走る競技追加
proc 69c78cb 玉関係の競技追加

### ブランチ切り替え
git checkout ブランチ
$ git checkout proc
$ git branch
$ cat program.txt
春の大運動会
* 開会式
* 玉入れ
* 大玉転がし
* 閉会式

$ git checkout master
$ git branch
$ cat program.txt
春の大運動会
* 開会式
* 玉入れ
* 大玉転がし
* 二人三脚
* 徒競走
* 閉会式

$ git log –all –graph
$ git log –all –graph –oneline
* df1adc9 (proc) 選手宣誓追加
| * 66006b7 (HEAD -> master) 走る競技追加
|/
* 69c78cb 玉関係の競技追加
* 38d5fe3 プログラム作成開始

### ブランチのマージ
$ git merge proc
$ cat program.txt
春の大運動会
* 開会式
* 選手宣誓
* 玉入れ
* 大玉転がし
* 二人三脚
* 徒競走
* 閉会式

$ git ls-files -s
100644 9dc5b70af547cf38876fd39e696ca12944609018 1 program.txt
100644 8dcdb3703137b61fd6286001320d39045e9ab679 2 program.txt
100644 1203c826d8f52af84d75362c8a59f69fa399a473 3 program.txt

### ブランチを削除
git branch -d ブランチ
$ git checkout master
$ git branch -d proc

なるほどー ブランチの概念はわかったわ

git基本コマンド2

git diff: ファイルの差分, 最後にstageした後に何を変更したかの確認によく使う
git diff –staged, git diff HEAD

$ git diff
diff –git a/hello.txt b/hello.txt
index 05a682b..4a49d86 100644
— a/hello.txt
+++ b/hello.txt
@@ -1 +1,2 @@
-Hello!
\ No newline at end of file
+Hello!
+I am a student.

### 履歴の中で違いを見る
git diff コミット1 コミット2
git diff コミット1 コミット2 — ファイル
$ git diff 7aad1 HEAD
$ git diff HEAD^ HEAD
$ git diff HEAD~1 HEAD
$ git diff @^^ @

headは@とも書ける

### コミット情報の表示
git show: 直近のコミットがどんなものだったかを表示する
$ git show

### 一度ステージした内容を変更
ファイルに変更を追加してgit add -uなどとする

### ステージの内容を見る
$ git ls-files

詳細を見る場合は-sオプションをつける
$ git ls-files -s
$ git show a5842752b

### ステージしたことを無かったことにする
git reset
git reset –file
git reset –hard   # ツリーの状態をHEADの上に戻す
$ git reset –hard
変更したものが、HEADの状態に戻る

### ステージした後の変更をなしにする
git checkout — file
$ git checkout — hello.txt
$ cat hello.txt

### ファイル名を変える
git mv 元のファイル 新しいファイル

### ファイルを管理下から外す
git rm ファイル

ステージだけから取り除き、作業ツリーにあるものは残したい場合 –cachedをつける
git rm –cached file
git rm -r –cached dir

### 昔のファイルをリポジトリから取り出す
$ git checkout HEAD~3 –hello.txt

チェックアウトせずに昔のファイルを見る
$ git show HEAD~3:hello.txt

$ git checkout HEAD~3 — hello.txt
$ git show HEAD~3:hello.txt

### 作業ツリーの内容をHEADの状態にする
$ git reset –hard
$ ls

### 最近の履歴を無かったものにする
$ git reset –hard コミット
$ git reset a82684a02
$ git log

### 直近のコミットをやり直す
$ git commit –amend

### gitにファイルを無視させる
.gitignoreを使う

*.log
!test.log

$ git commit -m “8th commit”

使いながら覚えないとどうにもならんな これ…

git基本コマンド1

$ git help
$ git help help

### git config
Gitの設定ファイルにある変数の値を設定
/etc/gitconfig : マシン全体に対する設定 –sytemオプションを使う
~/.gitconfig: ユーザごとの設定 –globalオプション
.git/config: プロジェクトごとの設定

$ git config –global -l
user.email=hoge@gmail.com
user.name=Hpscript

### バージョン管理を始める
$ git init
Initialized empty Git repository in /home/vagrant/dev/git/.git/

### ファイルやディレクトリを管理下に入れる
$ touch README.txt
$ git add README.text

### コミットしてリポジトリに入れる
既にステージにある場合は git add -uを使う
$ git commit -m “first commit”
$ git add -u
$ git commit -m “second commit”

### 履歴を見る
$ git log
$ git log –oneline
7aad1d5 (HEAD -> master) second commit
8a0580a first commit

### ファイルを追加
hello.txt
$ git add .

### どのファイルを新たにステージしたか見る
$ git status
※ファイルを追加した場合、new file, 編集した場合はmodified
$ git add -u
$ git status
※stageされてない場合は untracked fileと表示される

うむ、基礎操作から結構大変だな

[Docker] スタンドアローンSwarmのオーバーレイネットワーク

オーバーレイネットワーク: 複数のホストを跨いだネットワーキングに使用されるネットワークドライバ
L ホストのDockerの情報を保存しておくためのキーバリューストアと呼ばれるデータを保持するサーバが必要
L Consul, Etcd, Zookeeperなどがある

TCP port: 2377
TCP/UDP port: 7946
UDP port: 4789

うむ、これはよくわからんわ…

[Docker] ノンネットワークとホストネットワーク

ノンネットワーク: ネットワークインターフェイスを持たない状態にしたい場合に使用するネットワーク
ホストネットワーク: Dockerホストと同じネットワークインターフェースをコンテナで使用したい場合に使用するネットワーク

$ sudo docker network ls
562eb5e84daa none null local … none

$ sudo docker network disconnect my_nw ubuntu1
$ sudo docker network disconnect bridge ubuntu1
$ sudo docker network connect none ubuntu1
$ sudo docker inspect ubuntu1

                "none": {
                    "IPAMConfig": {},
                    "Links": null,
                    "Aliases": [],
                    "NetworkID": "562eb5e84daad5b15117ed125a41d3cffaaf9884095f55949dd92bf1a9533620",
                    "EndpointID": "30ccbdc2f734fede12c9bbd56b4e2b9fa27c0b988b5d7feca224548c4c8622ce",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": {}
                }

ホストネットワークの場合は、ポートフォワーディング設定を行わなくても直にnginxにアクセスできる

なるほど、インフラも奥が深いな…

[Docker] ユーザ定義のブリッジネットワーク作成

デフォルトのブリッジネットワークとユーザ定義のブリッジネットワーク

ubuntuコンテナを起動して詳細を確認します
$ sudo docker run -itd –name ubuntu1 ubuntu /bin/bash

$ sudo docker inspect ubuntu1
L ipアドレスの箇所で 172.17.0.2 が割り当てられている

            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "9d63e39880cdedc5addbd10a11deb3fad9b4679dca150c8ab24c6080a777d0a5",
                    "EndpointID": "d990aa1ffeb44365cc97df34730bf143b2a0b0afbe461fe811b877fca9ad390f",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }

もう一つ同じようにコンテナを作成する
$ sudo docker run -itd –name ubuntu2 ubuntu /bin/bash
$ sudo docker inspect ubuntu2
L ipアドレスの箇所で 172.17.0.3 が割り当てられている

            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "9d63e39880cdedc5addbd10a11deb3fad9b4679dca150c8ab24c6080a777d0a5",
                    "EndpointID": "ea06868987306ff7b8f7419a287d4a8d7282914f2c9746afbe991d9d26e2d657",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03",
                    "DriverOpts": null
                }
            }

$ sudo docker exec -ti 77573860f37e /bin/bash
$ apt-get update && apt install iputils-ping
root@77573860f37e:/# ping -c 3 172.17.0.3
PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.079 ms
64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.064 ms
64 bytes from 172.17.0.3: icmp_seq=3 ttl=64 time=0.099 ms

— 172.17.0.3 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2027ms
rtt min/avg/max/mdev = 0.064/0.080/0.099/0.014 ms
root@77573860f37e:/# ping -c 3 ubuntu2
ping: ubuntu2: Name or service not known

### コンテナ名で名前解決する方法
$ sudo docker network create my_nw
$ sudo docker network connect my_nw ubuntu1
$ sudo docker network connect my_nw ubuntu2
$ sudo docker exec -ti 77573860f37e /bin/bash
root@77573860f37e:/# ping -c 3 ubuntu2
PING ubuntu2 (172.22.0.3) 56(84) bytes of data.
64 bytes from ubuntu2.my_nw (172.22.0.3): icmp_seq=1 ttl=64 time=0.141 ms
64 bytes from ubuntu2.my_nw (172.22.0.3): icmp_seq=2 ttl=64 time=0.083 ms
64 bytes from ubuntu2.my_nw (172.22.0.3): icmp_seq=3 ttl=64 time=0.068 ms

おおおおおおお

$ sudo docker network ls

Docker基礎2

Dockerコンテナの状態
– created, running, exited, removing, paused, restarting, deadの状態がある

### コンテナへのシェル接続
$ sudo docker run -tid alpine /bin/sh

-tフラグ: 擬似ターミナル(peudo-TTY)を割り当てる
-iフラグ: アタッチされていない状態でも標準入力を保持するためのフラグ
-dフラグ: detachedモードで動作させる場合のフラグ

うむ、なるほど、基礎中の基礎はわかってきた。