Skip to content

Commit 273edfe

Browse files
committed
Add section on resolving conflicts in Git
1 parent e3370fc commit 273edfe

File tree

1 file changed

+263
-0
lines changed

1 file changed

+263
-0
lines changed

index.qmd

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,269 @@ git pull origin main
533533

534534
- git はコードの細かな変更を追跡し、共有を可能にします。
535535

536+
# git におけるコンフリクトの解決
537+
538+
## 質問と目的
539+
540+
質問
541+
542+
- 自分の変更が他の人の変更とコンフリクトする場合はどうすればいいですか?
543+
544+
目的
545+
546+
- コンフリクトとは何か、いつ発生する可能性があるかを説明します。
547+
- マージによって生じる競合を解決します。
548+
549+
## コンフリクトを作る (コラボレーターが編集)
550+
551+
コラボレーターの方にだけ 1 行追加してみましょう。
552+
553+
``` bash
554+
$ nano mars.txt
555+
$ cat mars.txt
556+
```
557+
558+
```
559+
Cold and dry, but everything is my favorite color
560+
The two moons may be a problem for Wolfman
561+
But the Mummy will appreciate the lack of humidity
562+
This line added to Wolfman's copy
563+
```
564+
565+
## コンフリクトを作る (コラボレーターがコミット)
566+
567+
コラボレーターの方で変更をコミットします。
568+
569+
``` bash
570+
$ git add mars.txt
571+
$ git commit -m "Add a line in our home copy"
572+
```
573+
574+
```
575+
[main 5ae9631] Add a line in our home copy
576+
1 file changed, 1 insertion(+)
577+
```
578+
579+
## コンフリクトを作る (コラボレーターがプッシュ)
580+
581+
コラボレーターの方でコミットを GitHub にプッシュします。
582+
583+
``` bash
584+
$ git push origin main
585+
```
586+
587+
```
588+
Enumerating objects: 5, done.
589+
Counting objects: 100% (5/5), done.
590+
Delta compression using up to 8 threads
591+
Compressing objects: 100% (3/3), done.
592+
Writing objects: 100% (3/3), 331 bytes | 331.00 KiB/s, done.
593+
Total 3 (delta 2), reused 0 (delta 0)
594+
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
595+
To https://github.com/vlad/planets.git
596+
29aba7c..dabb4c8 main -> main
597+
```
598+
599+
## コンフリクトを作る (オーナーが編集)
600+
601+
オーナーが GitHub から pull せずに、自分のコピーに「別の」変更を加えてもらいます。
602+
603+
``` bash
604+
$ nano mars.txt
605+
$ cat mars.txt
606+
```
607+
608+
```
609+
Cold and dry, but everything is my favorite color
610+
The two moons may be a problem for Wolfman
611+
But the Mummy will appreciate the lack of humidity
612+
We added a different line in the other copy
613+
```
614+
615+
## コンフリクトを作る (オーナーがコミット)
616+
617+
オーナーは変更をローカルにコミットできます:
618+
619+
``` bash
620+
$ git add mars.txt
621+
$ git commit -m "Add a line in my copy"
622+
```
623+
624+
```
625+
[main 07ebc69] Add a line in my copy
626+
1 file changed, 1 insertion(+)
627+
```
628+
629+
## コンフリクトを作る (オーナーがプッシュ「できない」)
630+
631+
しかし、オーナーは Git で GitHub にプッシュできません。
632+
633+
``` bash
634+
$ git push origin main
635+
```
636+
637+
```
638+
To https://github.com/vlad/planets.git
639+
! [rejected] main -> main (fetch first)
640+
error: failed to push some refs to 'https://github.com/vlad/planets.git'
641+
hint: Updates were rejected because the remote contains work that you do
642+
hint: not have locally. This is usually caused by another repository pushing
643+
hint: to the same ref. You may want to first integrate the remote changes
644+
hint: (e.g., 'git pull ...') before pushing again.
645+
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
646+
```
647+
648+
## コンフリクトのイメージ
649+
650+
![](https://swcarpentry-ja.github.io/git-novice/fig/conflict.svg)
651+
652+
Git は、リモート リポジトリにローカル ブランチに組み込まれていない新しい更新があることを検出したため、プッシュを拒否しました。
653+
654+
## コンフリクトの解決 (オーナーが git pull する)
655+
656+
必要なのは、GitHub から変更を pull し、現在作業中のコピーにマージしてからプッシュすることです。
657+
まずは pull から始めましょう:
658+
659+
``` bash
660+
$ git pull origin main
661+
```
662+
663+
```
664+
remote: Enumerating objects: 5, done.
665+
remote: Counting objects: 100% (5/5), done.
666+
remote: Compressing objects: 100% (1/1), done.
667+
remote: Total 3 (delta 2), reused 3 (delta 2), pack-reused 0
668+
Unpacking objects: 100% (3/3), done.
669+
From https://github.com/vlad/planets
670+
* branch main -> FETCH_HEAD
671+
29aba7c..dabb4c8 main -> origin/main
672+
Auto-merging mars.txt
673+
CONFLICT (content): Merge conflict in mars.txt
674+
Automatic merge failed; fix conflicts and then commit the result.
675+
```
676+
677+
## コンフリクトの解決 (拒否されたマージの結果)
678+
679+
git pullコマンドは、ローカル リポジトリを更新して、リモート リポジトリにすでに含まれている変更を組み込みます。
680+
リモート ブランチからの変更が取得された後、Git はローカル コピーに加えられた変更がリモート リポジトリに加えられた変更と重複していることを検出し、
681+
以前の作業が損なわれないように 2 つのバージョンのマージを拒否します。
682+
コンフリクトは、影響を受けるファイルで下記のようにマークされます。
683+
684+
``` bash
685+
$ cat mars.txt
686+
```
687+
688+
```
689+
Cold and dry, but everything is my favorite color
690+
The two moons may be a problem for Wolfman
691+
But the Mummy will appreciate the lack of humidity
692+
<<<<<<< HEAD
693+
We added a different line in the other copy
694+
=======
695+
This line added to Wolfman's copy
696+
>>>>>>> dabb4c8c450e8475aee9b14b4383acc99f42af1d
697+
```
698+
699+
## コンフリクトの解決 (マーカーを削除し、変更を調整する)
700+
701+
このファイルを編集して、これらのマーカーを削除し、変更を調整するのは私たちの仕事です。
702+
ローカル リポジトリでの変更を維持するか、リモート リポジトリでの変更を維持するか、両方を置き換える新しいものを書き込むか、変更を完全に削除するか、何でも好きなように行うことができます。
703+
両方を置き換えて、ファイルを次のようにしてみましょう。
704+
705+
```
706+
Cold and dry, but everything is my favorite color
707+
The two moons may be a problem for Wolfman
708+
But the Mummy will appreciate the lack of humidity
709+
We removed the conflict on this line
710+
```
711+
712+
## コンフリクトの解決 (マージを完了するために変更をコミットする)
713+
714+
``` bash
715+
$ git add mars.txt
716+
$ git status
717+
```
718+
719+
```
720+
On branch main
721+
All conflicts fixed but you are still merging.
722+
(use "git commit" to conclude merge)
723+
724+
Changes to be committed:
725+
726+
modified: mars.txt
727+
```
728+
729+
``` bash
730+
$ git commit -m "Merge changes from GitHub"
731+
```
732+
733+
```
734+
[main 2abf2b1] Merge changes from GitHub
735+
```
736+
737+
## コンフリクトの解決 (ついに push してコンフリクトを解決)
738+
739+
``` bash
740+
$ git push origin main
741+
```
742+
743+
```
744+
Enumerating objects: 10, done.
745+
Counting objects: 100% (10/10), done.
746+
Delta compression using up to 8 threads
747+
Compressing objects: 100% (6/6), done.
748+
Writing objects: 100% (6/6), 645 bytes | 645.00 KiB/s, done.
749+
Total 6 (delta 4), reused 0 (delta 0)
750+
remote: Resolving deltas: 100% (4/4), completed with 2 local objects.
751+
To https://github.com/vlad/planets.git
752+
dabb4c8..2abf2b1 main -> main
753+
```
754+
755+
## コンフリクトの解決 (今度はコラボレーターが pull する)
756+
757+
Git は何を何とマージしたかを追跡しているので、最初の変更を行ったコラボレーターが pull したときに、再度手作業で修正する必要がありません。
758+
今度はコラボレーターが pull してください
759+
760+
``` bash
761+
$ git pull origin main
762+
```
763+
764+
```
765+
remote: Enumerating objects: 10, done.
766+
remote: Counting objects: 100% (10/10), done.
767+
remote: Compressing objects: 100% (2/2), done.
768+
remote: Total 6 (delta 4), reused 6 (delta 4), pack-reused 0
769+
Unpacking objects: 100% (6/6), done.
770+
From https://github.com/vlad/planets
771+
* branch main -> FETCH_HEAD
772+
dabb4c8..2abf2b1 main -> origin/main
773+
Updating dabb4c8..2abf2b1
774+
Fast-forward
775+
mars.txt | 2 +-
776+
1 file changed, 1 insertion(+), 1 deletion(-)
777+
```
778+
779+
## コンフリクトの解決 (コラボレーターがオーナーにマージされた内容を確認)
780+
781+
コラボレーターはオーナーにマージされた内容を確認してみましょう
782+
783+
``` bash
784+
$ cat mars.txt
785+
```
786+
787+
```
788+
Cold and dry, but everything is my favorite color
789+
The two moons may be a problem for Wolfman
790+
But the Mummy will appreciate the lack of humidity
791+
We removed the conflict on this line
792+
```
793+
794+
## コンフリクトの解決のまとめ
795+
796+
- 2 人以上の人が同じファイルの同じ行を変更すると、競合が発生します。
797+
- バージョン管理システムでは、ユーザーが互いの変更を盲目的に上書きすることは許可されませんが、競合が強調表示されるので、解決できます。
798+
536799
# R と RStudio
537800

538801
## チャレンジ:変数名

0 commit comments

Comments
 (0)