LoginSignup
0
0

Gitの動作を理解するために、Gitのコマンドを実際に試して、結果を見てみました。

1つの記事内で一連のGitコマンドが完結しているので、これら一連のコマンドを順に実行させて結果を見ることで、一連のGitの動作を実際に体感でき、一通り独習することが可能です。

※前回記事のリポジトリ状態からの続きになっています。

前回記事へ 目次へ:Git関連記事のまとめページ 次回記事へ

実行例

(※例えばmasterを更新しておき、branch1・2から離れていく様子を模擬)
echo Sample-Added-110 >> test1.txt
echo Sample-Added-1010 > test10.txt
git add .
git commit -m "message10"

echo Sample-Added-111 >> test1.txt
echo Sample-Added-****21 > test2.txt
echo Sample-Added-1011 >> test10.txt
git add .
git commit -m "message11"

echo Sample-Added-1012 >> test10.txt
git add .
git commit -m "message12"

(※始めにブランチの形を確認)
git log --oneline --graph --all
↓
結果: 
* 60b0341 (HEAD -> master) message12
* 045a734 message11
* d10f934 message10
| * b838160 (branch2) message9
| * c68ef01 message8
|/
| * 2495cae (branch1) message7
|/
* 94996a0 message4
* 20bbab7 message3
* 00cad71 message2
* 4ace194 message1

※現在のブランチ構造の模式図
message1-2-3-4-10-11-12 ←master
             |
             +-7 ←branch1
             |
             +-8-9 ←branch2

-----

※Pro Git本 引用: Gitは無理をしてまでコンフリクトを解消しようとはしません。Gitは、マージの内容が明確かどうか正確に判断できるよう作られています。しかし、コンフリクトが発生した場合は、わかったつもりになってコンフリクトを解消してしまうようなことはしません。

-----

※branch1に、masterの更新分をマージする場合
git checkout branch1

※masterからマージされる予定の更新分を確認
git log branch1..master --oneline
↓
結果: 
60b0341 (master) message12
045a734 message11
d10f934 message10

※同様
git log ..master --oneline
↓
結果: 
60b0341 (master) message12
045a734 message11
d10f934 message10

git log branch1..master --oneline --stat
↓
結果: 
60b0341 (master) message12
 test10.txt | 1 +
 1 file changed, 1 insertion(+)
045a734 message11
 test1.txt  | 1 +
 test10.txt | 1 +
 test2.txt  | 2 +-
 3 files changed, 3 insertions(+), 1 deletion(-)
d10f934 message10
 test1.txt  | 1 +
 test10.txt | 1 +
 2 files changed, 2 insertions(+)

git log branch1..master --oneline -p -U1
↓
結果: 
60b0341 (master) message12
diff --git a/test10.txt b/test10.txt
index 166cf2a..002832f 100644
--- a/test10.txt
+++ b/test10.txt
@@ -2 +2,2 @@ Sample-Added-1010
 Sample-Added-1011
+Sample-Added-1012
045a734 message11
diff --git a/test1.txt b/test1.txt
index ab3d9a7..2b06b09 100644
--- a/test1.txt
+++ b/test1.txt
@@ -5 +5,2 @@ Sample-Added-14
 Sample-Added-110
+Sample-Added-111
diff --git a/test10.txt b/test10.txt
index 128c764..166cf2a 100644
--- a/test10.txt
+++ b/test10.txt
@@ -1 +1,2 @@
 Sample-Added-1010
+Sample-Added-1011
diff --git a/test2.txt b/test2.txt
index 1facb47..b727d07 100644
--- a/test2.txt
+++ b/test2.txt
@@ -1 +1 @@
-Sample-Added-**21
+Sample-Added-****21
d10f934 message10
diff --git a/test1.txt b/test1.txt
index bb7eb1a..ab3d9a7 100644
--- a/test1.txt
+++ b/test1.txt
@@ -4 +4,2 @@ Sample-Added-13
 Sample-Added-14
+Sample-Added-110
diff --git a/test10.txt b/test10.txt
new file mode 100644
index 0000000..128c764
--- /dev/null
+++ b/test10.txt
@@ -0,0 +1 @@
+Sample-Added-1010

-----

※branch1に、masterの更新分をマージ
git merge master
↓
結果: 
Auto-merging test1.txt
CONFLICT (content): Merge conflict in test1.txt
Automatic merge failed; fix conflicts and then commit the result.

git status
↓
結果: 
On branch branch1
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Changes to be committed:
        new file:   test10.txt
        modified:   test2.txt

Unmerged paths:
  (use "git add <file>..." to mark resolution)
        both modified:   test1.txt

git status -s
↓
結果: 
UU test1.txt
A  test10.txt
M  test2.txt

type test1.txt
↓
結果: 
Sample-Added-11
Sample-Added-12
Sample-Added-13
Sample-Added-14
<<<<<<< HEAD
Sample-Added-17
=======
Sample-Added-110
Sample-Added-111
>>>>>>> master

-----

※コンフリクト中のファイルに対して、マージ前後でどう変更されるかを表示する
※マージのコンフリクトが発生した後にgit diffを実行すると、Combined Diffという形式で表示される
git diff
↓
結果: 
diff --cc test1.txt
index 229df10,2b06b09..0000000
--- a/test1.txt
+++ b/test1.txt
@@@ -2,4 -2,5 +2,9 @@@ Sample-Added-1
  Sample-Added-12
  Sample-Added-13
  Sample-Added-14
++<<<<<<< HEAD
 +Sample-Added-17
++=======
+ Sample-Added-110
+ Sample-Added-111
++>>>>>>> master

※コンフリクト中のファイルに対して、マージ前後で自分ブランチ側ではどう変更されるか
git diff --ours
↓
結果: 
* Unmerged path test1.txt
diff --git a/test1.txt b/test1.txt
index 229df10..18f278a 100644
--- a/test1.txt
+++ b/test1.txt
@@ -2,4 +2,9 @@ Sample-Added-11
 Sample-Added-12
 Sample-Added-13
 Sample-Added-14
+<<<<<<< HEAD
 Sample-Added-17
+=======
+Sample-Added-110
+Sample-Added-111
+>>>>>>> master

※コンフリクト中のファイルに対して、マージ前後で相手ブランチ側ではどう変更されるか
git diff --theirs
↓
結果: 
* Unmerged path test1.txt
diff --git a/test1.txt b/test1.txt
index 2b06b09..18f278a 100644
--- a/test1.txt
+++ b/test1.txt
@@ -2,5 +2,9 @@ Sample-Added-11
 Sample-Added-12
 Sample-Added-13
 Sample-Added-14
+<<<<<<< HEAD
+Sample-Added-17
+=======
 Sample-Added-110
 Sample-Added-111
+>>>>>>> master

※コンフリクト中のファイルに対して、マージ前後で分岐点の状態からどう変更されるか
git diff --base
↓
結果: 
* Unmerged path test1.txt
diff --git a/test1.txt b/test1.txt
index bb7eb1a..18f278a 100644
--- a/test1.txt
+++ b/test1.txt
@@ -2,3 +2,9 @@ Sample-Added-11
 Sample-Added-12
 Sample-Added-13
 Sample-Added-14
+<<<<<<< HEAD
+Sample-Added-17
+=======
+Sample-Added-110
+Sample-Added-111
+>>>>>>> master

-----

※コンフリクトが起こるとは思っていなかった、今はまだ処理したくない、といった場合
git merge --abort
git status
↓
結果: 
On branch branch1
nothing to commit, working tree clean

-----

git merge master
echo XXXXX > test1.txt
git merge --abort
git status
↓
結果: 
On branch branch1
nothing to commit, working tree clean

-----

git merge master
echo XXXXX > test1.txt

※おかしくなったので、全て取り消して一度やり直す (前記と同様の効果)
git reset --hard HEAD
git status
↓
結果: 
On branch branch1
nothing to commit, working tree clean

-----

(※再度同じマージを行う)
git merge master
↓
結果: 
Auto-merging test1.txt
CONFLICT (content): Merge conflict in test1.txt
Automatic merge failed; fix conflicts and then commit the result.

-----

※現在マージ中となる両ブランチに含まれるコミットのうち、両ブランチの重複分を除いて表示
git log --oneline --left-right HEAD...MERGE_HEAD
↓
結果: 
> 60b0341 (master) message12
> 045a734 message11
> d10f934 message10
< 2495cae (HEAD -> branch1) message7

-----

※現在コンフリクトが発生しているファイルに対して、編集したコミットだけを表示
※平行して進行していた2つの開発作業がなぜコードの同じ部分を編集するに至ったか、コンフリクトの発生原因を確認する
git log --oneline --left-right --merge
↓
結果: 
> 045a734 message11
> d10f934 message10
< 2495cae (HEAD -> branch1) message7

git log --oneline --left-right --merge -p -U1
↓
結果: 
> 045a734 message11
diff --git a/test1.txt b/test1.txt
index ab3d9a7..2b06b09 100644
--- a/test1.txt
+++ b/test1.txt
@@ -5 +5,2 @@ Sample-Added-14
 Sample-Added-110
+Sample-Added-111
> d10f934 message10
diff --git a/test1.txt b/test1.txt
index bb7eb1a..ab3d9a7 100644
--- a/test1.txt
+++ b/test1.txt
@@ -4 +4,2 @@ Sample-Added-13
 Sample-Added-14
+Sample-Added-110
< 2495cae (HEAD -> branch1) message7
diff --git a/test1.txt b/test1.txt
index bb7eb1a..229df10 100644
--- a/test1.txt
+++ b/test1.txt
@@ -4 +4,2 @@ Sample-Added-13
 Sample-Added-14
+Sample-Added-17

-----

※コンフリクトの表記の中に、分岐点の状態(base)も記載する
git checkout --conflict=diff3 test1.txt
↓
結果: 
Recreated 1 merge conflict

type test1.txt
↓
結果: 
Sample-Added-11
Sample-Added-12
Sample-Added-13
Sample-Added-14
<<<<<<< ours
Sample-Added-17
||||||| base
=======
Sample-Added-110
Sample-Added-111
>>>>>>> theirs

-----

※コンフリクトを解消する前後でのgit diffの動作を見る
git diff
↓
結果: 
diff --cc test1.txt
index 229df10,2b06b09..0000000
--- a/test1.txt
+++ b/test1.txt
@@@ -2,4 -2,5 +2,9 @@@ Sample-Added-1
  Sample-Added-12
  Sample-Added-13
  Sample-Added-14
++<<<<<<< HEAD
 +Sample-Added-17
++=======
+ Sample-Added-110
+ Sample-Added-111
++>>>>>>> master

(※コンフリクトを手動で解消)
echo Sample-Added-11 > test1.txt
echo Sample-Added-12 >> test1.txt
echo Sample-Added-13 >> test1.txt
echo Sample-Added-14 >> test1.txt
echo Sample-Added-17+110+111 >> test1.txt
type test1.txt
↓
結果: 
Sample-Added-11
Sample-Added-12
Sample-Added-13
Sample-Added-14
Sample-Added-17+110+111

※コンフリクトを手動で解消してからgit diffを実行して確認
git diff
↓
結果: 
diff --cc test1.txt
index 229df10,2b06b09..0000000
--- a/test1.txt
+++ b/test1.txt
@@@ -2,4 -2,5 +2,4 @@@ Sample-Added-1
  Sample-Added-12
  Sample-Added-13
  Sample-Added-14
- Sample-Added-17
 -Sample-Added-110
 -Sample-Added-111
++Sample-Added-17+110+111

-----

git add .
git status
↓
結果: 
On branch branch1
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:
        modified:   test1.txt
        new file:   test10.txt
        modified:   test2.txt

※マージ処置をコミット
git commit --no-edit
↓
結果: 
[branch1 135f847] Merge branch 'master' into branch1

git show -s
↓
結果: 
commit 135f847ec431e2a07484d9a1b70a75022bb4e953 (HEAD -> branch1)
Merge: 2495cae 60b0341
Author: person1 <person1@abc.def>
Date:   Sun Jan 1 18:45:24 2023 +0900

    Merge branch 'master' into branch1

    # Conflicts:
    #       test1.txt

git log --oneline --graph --all
↓
結果: 
*   135f847 (HEAD -> branch1) Merge branch 'master' into branch1
|\
| * 60b0341 (master) message12
| * 045a734 message11
| * d10f934 message10
* | 2495cae message7
|/
| * b838160 (branch2) message9
| * c68ef01 message8
|/
* 94996a0 message4
* 20bbab7 message3
* 00cad71 message2
* 4ace194 message1

環境

Windows 10、PortableGit-2.40.0-64-bitを使用、全てローカルPC上で実施、GitHub等は不使用。

0
0
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
0
0