Gitの基礎練習 add branch merge

さらに練習

考えたこと

  • add の後、commit しないで add したら?
  • ブランチ作ってみる
  • マージしてみる


やってみる

現状確認

$ cat hoge.txt
hogehoge
fugafuga
foobar

$ git diff
diff --git a/hoge.txt b/hoge.txt
index dbd2fc8..0bbb31c 100644
--- a/hoge.txt
+++ b/hoge.txt
@@ -1,3 +1,3 @@
hogehoge
fugafuga
-hogefuga
+foobar ※インデックス != ワーキングツリー

add 実行

$ git add hoge.txt

$ git diff --cached
diff --git a/hoge.txt b/hoge.txt
index dbd2fc8..0bbb31c 100644
--- a/hoge.txt
+++ b/hoge.txt
@@ -1,3 +1,3 @@
hogehoge
fugafuga
-hogefuga
+foobar ※HEAD != インデックス

ファイル更新

$ cat hoge.txt
hogehoge
fugafuga
foofoo ※更新

$ git diff
diff --git a/hoge.txt b/hoge.txt
index 0bbb31c..a8d1faf 100644
--- a/hoge.txt
+++ b/hoge.txt
@@ -1,3 +1,3 @@
hogehoge
fugafuga
-foobar
+foofoo ※インデックス != ワーキングツリー

add 再実行

$ git add hoge.txt

$ git diff --cached
diff --git a/hoge.txt b/hoge.txt
index dbd2fc8..a8d1faf 100644
--- a/hoge.txt
+++ b/hoge.txt
@@ -1,3 +1,3 @@
hogehoge
fugafuga
-hogefuga
+foofoo ※HEAD != インデックス

コミット

$ git commit -m "Modified text"
[master 498eec9] Modified text
1 files changed, 1 insertions(+), 1 deletions(-)

$ git diff ※インデックス = ワーキングツリー

$ git diff --cached ※HEAD = インデックス

ここまでのまとめ

  • インデックスは、直近の add が有効
    • 当たり前か・・・

ブランチ作成

$ git branch branchA

$ git branch
branchA
* master

作成したブランチに切り替え

$ git branch
branchA
* master

$ git checkout branchA
Switched to branch 'branchA'

$ git branch
* branchA
master

ブランチ側でファイル更新〜コミット

$ cat hoge.txt ※現状の確認
hogehoge
fugafuga
foofoo

$ cat hoge.txt
hogehoge branch ※更新
fugafuga
barfoo
piyopiyo ※追加

$ git diff
diff --git a/hoge.txt b/hoge.txt
index a8d1faf..04c7b04 100644
--- a/hoge.txt
+++ b/hoge.txt
@@ -1,3 +1,4 @@
-hogehoge
+hogehoge branch
fugafuga
foofoo
+piyopiyo

$ git add hoge.txt

$ git diff --cached
diff --git a/hoge.txt b/hoge.txt
index a8d1faf..04c7b04 100644
--- a/hoge.txt
+++ b/hoge.txt
@@ -1,3 +1,4 @@
-hogehoge
+hogehoge branch
fugafuga
foofoo
+piyopiyo

$ git commit -m "Modified text for branchA" ※ブランチへのコミット
[branchA fa4fb89] Modified text for branchA
1 files changed, 2 insertions(+), 1 deletions(-)

メインに戻る

$ git branch
* branchA
master

$ git checkout master
Switched to branch 'master'

メインvsブランチの差分確認

$ cat hoge.txt
hogehoge ※更新されていない
fugafuga
foofoo

$ git diff branchA ※ブランチとの差分
diff --git a/hoge.txt b/hoge.txt
index 04c7b04..a8d1faf 100644
--- a/hoge.txt
+++ b/hoge.txt
@@ -1,4 +1,3 @@
-hogehoge branch
+hogehoge
fugafuga
foofoo
-piyopiyo

マージ

$ git merge branchA
Updating 498eec9..fa4fb89
Fast-forward
hoge.txt | 3 ++-
1 files changed, 2 insertions(+), 1 deletions(-)

$ cat hoge.txt
hogehoge branch
fugafuga
foofoo
piyopiyo

$ git diff branchA ※差分なし

ここまでのまとめ

  • git branch ブランチ名 ブランチの作成
  • git checkout ブランチ名 ブランチの切り替え
  • git merge ブランチ名 ブランチのマージ

その他

やってたら、こんなツイートが流れてきた。

参考 Git初心者用BootCampのようなものの演習資料 - 日々常々

ちょっとやってみよ。