In this exercise, you will intentionally create merge conflicts to learn how to resolve them. Follow the steps below to complete the exercise.
If you are doing this exercise without a partner, just repeat the steps first for student A, and then for student b, creating different branches with different changes.
-
Set Up Your Environment:
- Ensure you have the repository set up in Codespaces.
- Both students should clone the repository or fork it as per the collaborative workflow.
- This will work either as collaborators on one repository, or with a repository and a fork of the repository. The guide below is for a single repository, but just follow the forking guidelines if you are working on two repositories.
-
Create a New Branch:
- Each student should create a new branch from the
mainbranch. You can name your branchesfeature/student1andfeature/student2.
git checkout -b feature/student1
git checkout -b feature/student2
- Each student should create a new branch from the
-
Make Changes to
src/main.py:- In
src/main.py, both students will modify the same line of code. For example, change the greeting message in thegreetfunction.
Student 1:
def greet(): return "Hello from Student 1!"
Student 2:
def greet(): return "Hello from Student 2!"
- In
-
Commit Your Changes:
- After making the changes, commit them to your respective branches.
git add src/main.py git commit -m "Update greeting message in main.py" -
Push Your Changes:
- Push your changes to the remote repository.
git push origin feature/student1
git push origin feature/student2
-
Open Pull Requests:
- Each student should open a pull request (PR) from their feature branch to the
mainbranch.
- Each student should open a pull request (PR) from their feature branch to the
-
Merge the First Pull Request:
- One student should merge their pull request first. This will update the
mainbranch with their changes.
- One student should merge their pull request first. This will update the
-
Attempt to Merge the Second Pull Request:
- The second student will now attempt to merge their pull request. This should result in a merge conflict since both students modified the same line in
src/main.py.
- The second student will now attempt to merge their pull request. This should result in a merge conflict since both students modified the same line in
-
Identify the Conflict:
- Git will indicate that there is a conflict in
src/main.py. Open the file to see the conflict markers.
- Git will indicate that there is a conflict in
-
Resolve the Conflict:
- Edit
src/main.pyto resolve the conflict. You can choose one of the greetings or combine them as needed.
Example resolution:
def greet(): return "Hello from Student 1 and Student 2!"
- Edit
-
Mark the Conflict as Resolved:
- After resolving the conflict, add the file to the staging area.
git add src/main.py
-
Commit the Resolved Changes:
- Commit the changes to finalize the merge.
git commit -m "Resolve merge conflict in main.py" -
Complete the Merge:
- Push the resolved changes to the remote repository.
git push origin feature/student2
Congratulations! You have successfully created and resolved a merge conflict. This exercise has helped you understand how to handle conflicts in Git, which is an essential skill for collaborative development.