git で MR (Merge Request) を出してコンフリクトした時、feature branch にて master に対して rebase して、feature branch に master の変更を取り込んで、コンフリクトを解消するということを以前は良くやっていた。
$ git init
$ touch sample.txt
★ master にて以下を実施。
$ echo "This is first commit." > sample.txt
$ cat sample.txt
This is first commit.
$ git add sample.txt
$ git commit -m "First commit."
[master (root-commit) f0f9ade] First commit.
1 file changed, 1 insertion(+)
create mode 100644 sample.txt
★ branch1 を作成
$ git checkout -b branch1
Switched to a new branch 'branch1'
★ また master に戻って、master のコミットを進める。
$ git checkout master
Switched to branch 'master'
$ echo "Added in master branch." >> sample.txt
$ cat sample.txt
This is first commit.
Added in master branch.
$ git add sample.txt
$ git commit -m "Added a line in master branch."
[master 8575158] Added a line in master branch.
1 file changed, 1 insertion(+)
★ branch1 に戻りコミットを進める。
$ git checkout branch1
Switched to branch 'branch1'
$ cat sample.txt
This is first commit.
$ echo "Added in branch1." >> sample.txt
$ cat sample.txt
This is first commit.
Added in branch1.
$ git add sample.txt
$ git commit -m "Added a line in branch1."
[branch1 e8b2823] Added a line in branch1.
1 file changed, 1 insertion(+)
★ここまでが準備。ここから本題の rebase を用いたコンフリクトの解消。
branch1 にて master に対して rebase を実行する。
$ git rebase master
Auto-merging sample.txt
CONFLICT (content): Merge conflict in sample.txt
error: could not apply e8b2823... Added a line in branch1.
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply e8b2823... Added a line in branch1.
$ cat sample.txt
This is first commit.
<<<<<<< HEAD
Added in master branch.
=======
Added in branch1.
>>>>>>> e8b2823 (Added a line in branch1.)
★以下で sample.txt を編集してコンフリクトを解消。
$ vi sample.txt
$ cat sample.txt
This is first commit.
Added in master branch.
Added in branch1.
$ git add sample.txt
$ git rebase --continue
[detached HEAD 697c015] Added a line in branch1.
1 file changed, 1 insertion(+)
Successfully rebased and updated refs/heads/branch1.
$ git status
On branch branch1
nothing to commit, working tree clean
$ git log
commit 697c015003f2e61507a00c3d37769833e50e0ff3 (HEAD -> branch1)
Author: xxxxx <you@example.com>
Date: Sun Nov 19 16:38:57 2023 +0900
Added a line in branch1.
commit 857515894d3f4e5b3a0bc90b4c38da23bd2517e4 (master)
Author: xxxxx <you@example.com>
Date: Sun Nov 19 16:37:16 2023 +0900
Added a line in master branch.
commit f0f9adebf3ffc3c5b4ade40069c79d353a108206
Author: xxxxx <you@example.com>
Date: Sun Nov 19 16:34:42 2023 +0900
First commit.
★以上で rebase を用いたコンフリクトの解消が完了。
master に branch1 を繋げるかたちなので、First commit -> master での変更 -> branch1 での変更 という順になっている。