概要
git merge
で--squash
オプションを付けると、指定したブランチの全コミットを1つにまとめて今のブランチに追加する。
準備
以下のようなリポジトリーを使う。
main
ブランチのコミットでREADME.txt
に新規書き込みtopic
ブランチの2つのコミットでREADME.txt
に新規書き込みと1行追加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
[vagrant@localhost example]$ git log --all --graph -p * commit 4a650fbf10788e751ec4adafbcc262e6fafbd91a (topic) | Author: taustation <taustation@gmail.com> | Date: Sat Aug 14 11:58:57 2021 +0900 | | topic-2 | | diff --git a/README.md b/README.md | index 61e0c69..6bf2b17 100644 | --- a/README.md | +++ b/README.md | @@ -1 +1,2 @@ | topicブランチで新規書き込み | +topicブランチで1行追加 | * commit 81b58182e88d1bb6de3f5c072c23346443e8f473 | Author: taustation <taustation@gmail.com> | Date: Sat Aug 14 11:57:44 2021 +0900 | | topic-1 | | diff --git a/README.md b/README.md | index c6fba08..61e0c69 100644 | --- a/README.md | +++ b/README.md | @@ -1 +1 @@ | -# example | \ No newline at end of file | +topicブランチで新規書き込み | | * commit d181ff88ca5d18c2aa2795fee0a8596cb8a3e533 (HEAD -> main) |/ Author: taustation <taustation@gmail.com> | Date: Sat Aug 14 11:56:15 2021 +0900 | | main-1 | | diff --git a/README.md b/README.md | index c6fba08..c133df9 100644 | --- a/README.md | +++ b/README.md | @@ -1 +1 @@ | -# example | \ No newline at end of file | +mainブランチで新規書き込み | * commit 7fe38cd822113861b9e2062902fea7881f10de45 (origin/main, origin/HEAD) Author: taustation <88479749+taustation@users.noreply.github.com> Date: Sat Aug 14 11:43:19 2021 +0900 Initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..c6fba08 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# example \ No newline at end of file |
マージ
main
ブランチで、--suquash
オプションを指定してgit merge
でtopic
ブランチをマージ。今回は同じREADME.txt
を編集しているので競合が発生している。
1 2 3 4 5 6 7 8 |
[vagrant@localhost example]$ git branch * main topic [vagrant@localhost example]$ git merge --squash topic Auto-merging README.md CONFLICT (content): Merge conflict in README.md Squash commit -- not updating HEAD Automatic merge failed; fix conflicts and then commit the result. |
README.txt
の競合状態を確認。
1 2 3 4 5 6 7 |
[vagrant@localhost example]$ cat README.md <<<<<<< HEAD mainブランチで新規書き込み ======= topicブランチで新規書き込み topicブランチで1行追加 >>>>>>> topic |
README.txt
を編集して競合を解消。
1 2 3 4 5 |
[vagrant@localhost example]$ vi README.md [vagrant@localhost example]$ cat README.md mainブランチで新規書き込み topicブランチで新規書き込み topicブランチで1行追加 |
結果をadd、コミット。
1 2 3 4 5 6 |
[vagrant@localhost example]$ git add . [vagrant@localhost example]$ git commit [main 3ba8418] Squashed commit of the following: 1 file changed, 2 insertions(+) [vagrant@localhost example]$ [main 3ba8418] Squashed commit of the following: 1 file changed, 2 insertions(+) |
no-fast-forwardマージとしてエディターが起動。内容を確認して保存・終了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
Squashed commit of the following: commit 4a650fbf10788e751ec4adafbcc262e6fafbd91a Author: taustation <taustation@gmail.com> Date: Sat Aug 14 11:58:57 2021 +0900 topic-2 commit 81b58182e88d1bb6de3f5c072c23346443e8f473 Author: taustation <taustation@gmail.com> Date: Sat Aug 14 11:57:44 2021 +0900 topic-1 # Conflicts: # README.md # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # On branch main # Your branch is ahead of 'origin/main' by 1 commit. # (use "git push" to publish your local commits) # # Changes to be committed: # modified: README.md # ~ ~ ~ ..... |
コミット結果が表示される。
1 2 3 4 5 |
[vagrant@localhost example]$ git commit [main 3ba8418] Squashed commit of the following: 1 file changed, 2 insertions(+) [vagrant@localhost example]$ [main 3ba8418] Squashed commit of the following: 1 file changed, 2 insertions(+) |
ログで確認。topic
ブランチでの2つのコミットが1つのsquashed commit
としてつなげられた。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
[vagrant@localhost example]$ git log --all --graph * commit 3ba84184f84e24a5d4caffa2b028efcbb6b30850 (HEAD -> main) | Author: taustation <taustation@gmail.com> | Date: Sat Aug 14 13:36:47 2021 +0900 | | Squashed commit of the following: | | commit 4a650fbf10788e751ec4adafbcc262e6fafbd91a | Author: taustation <taustation@gmail.com> | Date: Sat Aug 14 11:58:57 2021 +0900 | | topic-2 | | commit 81b58182e88d1bb6de3f5c072c23346443e8f473 | Author: taustation <taustation@gmail.com> | Date: Sat Aug 14 11:57:44 2021 +0900 | | topic-1 | * commit d181ff88ca5d18c2aa2795fee0a8596cb8a3e533 | Author: taustation <taustation@gmail.com> | Date: Sat Aug 14 11:56:15 2021 +0900 | | main-1 | | * commit 4a650fbf10788e751ec4adafbcc262e6fafbd91a (topic) | | Author: taustation <taustation@gmail.com> | | Date: Sat Aug 14 11:58:57 2021 +0900 | | | | topic-2 | | | * commit 81b58182e88d1bb6de3f5c072c23346443e8f473 |/ Author: taustation <taustation@gmail.com> | Date: Sat Aug 14 11:57:44 2021 +0900 | | topic-1 | * commit 7fe38cd822113861b9e2062902fea7881f10de45 (origin/main, origin/HEAD) Author: taustation <88479749+taustation@users.noreply.github.com> Date: Sat Aug 14 11:43:19 2021 +0900 Initial commit |
マージの取り消し
git reset --hard ORIG_HEAD
でマージ前に戻ることができる。
1 |
[vagrant@localhost example]$ git reset --hard ORIG_HEAD |
ログも元に戻る。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
[vagrant@localhost example]$ git log --all --graph * commit 4a650fbf10788e751ec4adafbcc262e6fafbd91a (topic) | Author: taustation <taustation@gmail.com> | Date: Sat Aug 14 11:58:57 2021 +0900 | | topic-2 | * commit 81b58182e88d1bb6de3f5c072c23346443e8f473 | Author: taustation <taustation@gmail.com> | Date: Sat Aug 14 11:57:44 2021 +0900 | | topic-1 | | * commit d181ff88ca5d18c2aa2795fee0a8596cb8a3e533 (HEAD -> main) |/ Author: taustation <taustation@gmail.com> | Date: Sat Aug 14 11:56:15 2021 +0900 | | main-1 | * commit 7fe38cd822113861b9e2062902fea7881f10de45 (origin/main, origin/HEAD) Author: taustation <88479749+taustation@users.noreply.github.com> Date: Sat Aug 14 11:43:19 2021 +0900 Initial commit |