gitでわざとコンフリクトを発生させてみる
概要
gitの勉強の一つとしてコンフリクトをわざと発生させる手順を学びたいと思います。
環境
Git 2.13.1
Windows8
Bitbucket
詳細
今回は、2つのローカルリポジトリを作ってそれぞれにtestAブランチ、testBブランチを
作成して進行していきたいと思います。
コンフリクトを発生させるファイルをリモートリポジトリにpushする
ローカル側のtestAブランチを使ってコンフリクトを発生させるファイルをリモートリポジトリにpushします。
まずは、ファイルの作成からです。
touch test{1..10}.txt
ls
test1.txt test2.txt test4.txt test6.txt test8.txt
test10.txt test3.txt test5.txt test7.txt test9.txt
これらベースとなるファイルをpushするのでaddしてcommitします。
git add .
git commit -m testファイルを大量に追加
git log -n1
commit a78cdfde3bc11fb69de3e40760f6018722a5a582 (HEAD -> testA)
Author: kashiwara <------------>
Date: Sun Oct 8 13:04:24 2017 +0900
testファイルを大量に追加
commitできたのでpushしましょう。
git push origin testA
Bitbucket(リモート)の方にpushされてきたtestAブランチがいるのでマージします。
これでベースとなるファイルがリモートリポジトリのmasterにマージされた状態となりました。
testBブランチにファイルを取り込む
次にtestBブランチに先ほどtestAブランチとマージしたmasterブランチをpullしたいと思います。
git pull origin master
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
From -----------------
* branch master -> FETCH_HEAD
7c1b2da..140f412 master -> origin/master
Merge made by the 'recursive' strategy.
test1.txt | 0
test10.txt | 0
test2.txt | 0
test3.txt | 0
test4.txt | 0
test5.txt | 0
test6.txt | 0
test7.txt | 0
test8.txt | 0
test9.txt | 0
10 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test1.txt
create mode 100644 test10.txt
create mode 100644 test2.txt
create mode 100644 test3.txt
create mode 100644 test4.txt
create mode 100644 test5.txt
create mode 100644 test6.txt
create mode 100644 test7.txt
create mode 100644 test8.txt
create mode 100644 test9.txt
これでtestAブランチとtestBブランチが全く同じファイルを持つ状況となりました。
各、ブランチに変更を加えてcommitする
testAブランチに変更を加えます。
echo "oeiua" > test1.txt
echo "あ" > test2.txt
rm -rf test4.txt
test1.txtにoeuiaと入力し、test2.txtには文字を入力して残し、test4を消しました。
この変更をaddしてcommitしましょう。
git add .
git commit -m test1-2.txtを書き換えtest4.txtを消した
git log -n1
commit 2db675eefb2623f4332cf3fbfab270c6bf80adf3 (HEAD -> testA)
Author: kashiwara <------------>
Date: Sun Oct 8 13:56:24 2017 +0900
test1-2.txtを書き換えtest4.txtを消した
次は、testBブランチに変更を加えていきましょう。
echo "aiueo" > test1.txt
rm -rf test{2..4}.txt
test1.txtにaiueoと入力し、test2.txtからtest4.txtを消しました。
この変更をaddしてcommitしましょう。
git add .
git commit -m test1.txtを書き換えtest2-4.txtを消した
git log -n1
commit 3c1b2ce408b915d630255a9bf065333c6e951f07 (HEAD -> testB)
Author: kashiwara <------------>
Date: Sun Oct 8 13:36:17 2017 +0900
test1.txtを書き換えtest2-4.txtを消した
これでコンフリクトさせるための準備が整いました。
コンフリクトを発生させる
変更したtestAブランチをpushします
git push origin testA
testBブランチで変更をpullしてコンフリクトを起こしたいと思います。
git pull origin master
すると。。。コンフリクトが発生しました!!
git statusコマンドを叩いて確認してみましょう。
git status
On branch testB
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add/rm <file>..." as appropriate to mark resolution)
both modified: test1.txt
deleted by us: test2.txt
no changes added to commit (use "git add" and/or "git commit -a")
both modified : 編集内容によるコンフリクト
deleted by us : 自分のブランチ(testBブランチ)で削除したファイルによるコンフリクト
をそれぞれ意味します。
これらコンフリクトを解決しましょう。
コンフリクトの解決
コンフリクトしたtest1.txtをcatコマンドで見てみます。
cat test1.txt
<<<<<<< HEAD
aiueo
=======
oeuia
>>>>>>> 709ad198fbeb86aa3e3f903ab55f73dc19f71fbc
vimを使ってaiueoだけ残して保存します。
vi test1.txt
これで、編集内容によるコンフリクトは解決しました。
次に削除ファイルによるコンフリクトを解決します。
今回、test2.txtは残したいと思います。
残す場合は、普通にaddしてOKです。(new fileとして追加されるので)
git add .
git status
On branch testB
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
new file: test2.txt
これでcommitします。
git commit -m "コンフリクトをなおした"
git log -n1
commit 565196b735da345df82a5f0a6b005534ad054917 (HEAD -> testB)
Merge: 3c1b2ce 709ad19
Author: kashiwara <------------>
Date: Sun Oct 8 14:35:13 2017 +0900
コンフリクトをなおした
コンフリクトをなおしてcommitすることが出来ました。
以上です。