0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Git] 動作を試す 実行例37:-Xours・-XtheirsでOur・Theirsの選択をしたマージを行う

Posted at

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

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

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

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

実行例

(※branchX1とbranchX2のマージを行う用に、適当に変更の操作を追加)
git checkout branch1
git checkout -b branchX1
echo Sample-Added-X1 >> test1.txt
git commit -a -m "message X1"

echo Sample-Added-****X1 > test2.txt
echo Sample-Added-X1 > testX1.txt
git add .
git commit -a -m "message X2"

git checkout branch1
git checkout -b branchX2
echo Sample-Added-X2 >> test1.txt
git commit -a -m "message X3"

echo Sample-Added-****X2 > test2.txt
echo Sample-Added-X2 > testX2.txt
git add .
git commit -a -m "message X4"

git log --oneline --graph --all -5
↓
結果: 
* 2015668 (HEAD -> branchX2) message X4
* e97b00a message X3
| * 579bed2 (branchX1) message X2
| * e8a31d4 message X1
|/
*   2b19eaa (branch1) Merge branch 'master' into branch1
|\

-----

(※マージ: branchX1 ← branchX2 -Xours付き)

git checkout branchX1

※競合発生時は自分側を優先して解決するマージ
git merge -Xours branchX2
↓
結果: 
Auto-merging test1.txt
Auto-merging test2.txt
Merge made by the 'ort' strategy.
 testX2.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 testX2.txt

※1つ前との変更点を見る
git diff "HEAD^"
↓
結果: 
diff --git a/testX2.txt b/testX2.txt
new file mode 100644
index 0000000..654576f
--- /dev/null
+++ b/testX2.txt
@@ -0,0 +1 @@
+Sample-Added-X2

(※競合発生時は自分側を優先するので、test1.txt、test2.txtは自分側の内容のまま変更なしとなる)

git log --oneline --graph --all -6
↓
結果: 
*   90df4b9 (HEAD -> branchX1) Merge branch 'branchX2' into branchX1
|\
| * 2015668 (branchX2) message X4
| * e97b00a message X3
* | 579bed2 message X2
* | e8a31d4 message X1
|/
*   2b19eaa (branch1) Merge branch 'master' into branch1
|\

-----

(※マージ前の状態に戻す)
git reset --hard "HEAD^"
git log --oneline --graph --all -5
↓
結果: 
* 2015668 (branchX2) message X4
* e97b00a message X3
| * 579bed2 (HEAD -> branchX1) message X2
| * e8a31d4 message X1
|/
*   2b19eaa (branch1) Merge branch 'master' into branch1
|\

-----

(※マージ: branchX1 ← branchX2 -Xtheirs付き)

※競合発生時は相手側を優先して解決するマージ
git merge -Xtheirs branchX2
↓
結果: 
Auto-merging test1.txt
Auto-merging test2.txt
Merge made by the 'ort' strategy.
 test1.txt  | 2 +-
 test2.txt  | 2 +-
 testX2.txt | 1 +
 3 files changed, 3 insertions(+), 2 deletions(-)
 create mode 100644 testX2.txt

※1つ前との変更点を見る
git diff "HEAD^"
↓
結果: 
diff --git a/test1.txt b/test1.txt
index 41b8dae..bb32af9 100644
--- a/test1.txt
+++ b/test1.txt
@@ -7,4 +7,4 @@ Sample-Added-18
 Sample-Added-19
 Sample-Added-110
 Sample-Added-111
-Sample-Added-X1
+Sample-Added-X2
diff --git a/test2.txt b/test2.txt
index c83bd98..875aa00 100644
--- a/test2.txt
+++ b/test2.txt
@@ -1 +1 @@
-Sample-Added-****X1
+Sample-Added-****X2
diff --git a/testX2.txt b/testX2.txt
new file mode 100644
index 0000000..654576f
--- /dev/null
+++ b/testX2.txt
@@ -0,0 +1 @@
+Sample-Added-X2

(※競合発生時は相手側を優先するので、test1.txt、test2.txtは相手側の内容へそのまま更新となる)

git log --oneline --graph --all -6
↓
結果: 
*   2a5cd04 (HEAD -> branchX1) Merge branch 'branchX2' into branchX1
|\
| * 2015668 (branchX2) message X4
| * e97b00a message X3
* | 579bed2 message X2
* | e8a31d4 message X1
|/
*   2b19eaa (branch1) Merge branch 'master' into branch1
|\

-----

(※マージ前の状態に戻す)
git reset --hard "HEAD^"

-----

(※マージ: branchX1 ← branchX2 -s ours付き)

※全て自分側を優先して自分側の内容のままでマージ(ダミーでマージ済みとする用途)
git merge -s ours branchX2
↓
結果: 
Merge made by the 'ours' strategy.

※1つ前との変更点を見る
git diff "HEAD^"
↓
結果: 
(なし)

(※自分側の内容のままで、変更点なしとなる)

git log --oneline --graph --all -6
↓
結果: 
*   9e106c8 (HEAD -> branchX1) Merge branch 'branchX2' into branchX1
|\
| * 2015668 (branchX2) message X4
| * e97b00a message X3
* | 579bed2 message X2
* | e8a31d4 message X1
|/
*   2b19eaa (branch1) Merge branch 'master' into branch1
|\

-----

(※マージ前の状態に戻す)
git reset --hard "HEAD^"

※通常のマージではコンフリクトが発生
git merge branchX2
↓
結果: 
Auto-merging test1.txt
CONFLICT (content): Merge conflict in test1.txt
Auto-merging test2.txt
CONFLICT (content): Merge conflict in test2.txt
Automatic merge failed; fix conflicts and then commit the result.

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

※マージ状態を解除(取り消し)
git merge --abort

-----

(※始めの状態に戻す)
git checkout branch1
git branch -D branchX1
git branch -D branchX2

git log --oneline --graph --all -5
↓
結果: 
*   2b19eaa (HEAD -> branch1) Merge branch 'master' into branch1
|\
| * a4d1247 (master) message13 (fixed test10.txt)
| *   4791df6 Merge branch 'branch2'
| |\
| | * f3ef64d (branch2) message12
| | * 7fdeb04 message10

(※各ブランチを前回状態のコミット位置に移動させるだけでOK)

環境

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?