Let's try to create a merge conflict, and fix it together. You and a partner will each create separate branches, create a file with the same name, and then try to merge. The first will merge cleanly, the second will have a merge conflict. Work together to resolve the merge conflict.
- In our class repository, create the branch that you will be working on and name it something memorable like
USERNAME-conflict
. - Choose a file that both you and your partner will edit. (One of your files from earlier would work well.) On your branch, edit that file. The filename must be the same filename that your partner uses. Make sure the content inside the file is different, and that neither file is empty.
- Create a pull request in the class repository with
base: main
andcompare: USERNAME-conflict
. - You will see that the first pull request can merge well.
- When you see the merge conflict in the second pull request, work together to resolve the merge conflict.
- Working locally, merge
main
into the feature branch. - When you see there's a conflict, that's OK! The files that have conflicts are listed under
Unmerged Paths
. Typegit status
to verify which file has the conflict. - Open that file in your text editor, and look for the merge conflict markers. (
<<<<<<<
,=======
,>>>>>>>
) - Both branches' versions of code are present - pick which one you want to keep, and save the changes.
- Add and commit the saved changes to resolve the merge conflict.
- Push the feature branch up to the remote, and see the resolution in the pull request.
- Working locally, merge
- Merge the pull request.
If it's difficult to practice with a partner, you can do that by alone by following below.
-
In our class repository, confirm the branch you are in is
main
. -
Create a branch and name it as
USERNAME-modify-first
. -
In the
USERNAME-modify-first
branch, edit your caption on line 6 of your slide file_slides/##-USERNAME.md
and commit it. -
Change the branch to
main
again, create a second branch and name it asUSERNAME-modify-conflict
. -
In the
USERNAME-modify-conflict
branch, edit your caption with different string on the same position as the previous step (line 6 of_slides/##-USERNAME.md
) and commit it.- Make sure the content inside of the file is different, and that neither file is empty.
-
Create a pull request in the class repository with
base: main
andcompare: USERNAME-modify-first
. (title:USERNAME merge first
) -
Create a pull request in the class repository with
base: main
andcompare: USERNAME-modify-conflict
. (title:USERNAME resolve conflict
) -
You will see that the first pull request titled
USERNAME merge first
can merge well. -
When you see the merge conflict in the second pull request titled
USERNAME resolve conflict
, work by yourself to resolve the merge conflict.-
Working locally, switch to
main
branch and pull updates from remote.git switch main git pull
-
Fetch the
USERNAME-modify-conflict
branch from remote and switch to the same branch locally.git fetch git switch USER-modify-conflict
-
Merge
main
into the feature branch.git merge main
-
When you see there's a conflict, that's OK! The files that have conflicts are listed under
Unmerged Paths
. Typegit status
to verify which file has the conflict.git status
-
Open that file in your text editor, and look for the merge conflict markers. (
<<<<<<<
,=======
,>>>>>>>
)# Open the file on Visual Studio Code code _slides/##-USERNAME.md
-
Both branches' versions of code are present - pick which one you want to keep, and save the changes.
-
Add and commit the saved changes to resolve the merge conflict.
git add _slides/##-USERNAME.md git commit
-
Push the feature branch up to the remote, and see the resolution in the pull request.
git push
-
-
Confirm that the merge conflict in the branch titled
USERNAME resolve conflict
was resolved and merge the pull request.
What is a merge message? In this example, we are doing a recursive merge. A recursive merge creates a new commit that permanently records the point in time when these two branches were merged together. We will talk more about Git merge strategies a little later.