Imagine this: A group of friends, tired from their busy university schedules, decides to take a much-needed break. They plan a weekend getaway to the mountains, where they can relax, hike, and enjoy nature.
To make sure they don’t forget anything important, they create a shared packing list in a Git repository called “Weekend Out.”
Each friend has their own ideas about what to bring. Bob thinks they need extra blankets for the chilly nights, while Alice insists on packing a portable grill for a barbecue. Carol, the group’s tech enthusiast, wants to bring a drone to capture stunning aerial shots of their adventure.
To keep things organized, they decide to use Git to manage their packing list.
They create branches for different categories of items: feature/blankets
, feature/grill
, and feature/drone
.
Each friend adds their items to the list and commits their changes.
However, as they merge their branches into the main list, they encounter a problem: merge conflicts!
Bob and Alice both added items to the same line in the packing_list.md
file.
Now, they need to resolve these conflicts to ensure everyone’s items are included.
Luckily, they have learned about Git and how to resolve conflicts through the following exercises:
First, create a new repository using the Weekend Out repository as a template, aka forking. You can easily do this via the Use this template green drop-down list located on the top-right of this page.
Clone the repository to your local machine and navigate to the project directory. Don't forget to add your username in the directory path!
git clone git@github.com:<username>/Weekend-Out.git
cd Weekend-Out
Create a new feature/blankets
branch and add the items Bob wants to bring.
git checkout -b feature/blankets
open the packing_list.md
file, add some items and save the file.
Commit your changes and look at the log to see the commit history.
git add packing_list.md
git commit -m "Add blankets to the packing list"
git log --oneline
Get a feeling for the repository and its commit history.
- What is the status of the repository after the commit? On which branch are you now?
Play around with the log command to see the commit history in different formats. Check the main page for an overview of the available options. For example:
git log --oneline --graph
git log --oneline --author="Alice"
git log --oneline --since="2 days ago"
- What information is displayed for each commit?
- What is the commit hash and what is its purpose?
- What's the difference between
git log
andgit reflog
?
Push the branch to the remote repository.
git push origin feature/blankets
- What does the
origin
refer to in thegit push
command? Hint:git remote -v
. - What would be the result of the
git push
command if you didn't specify the branch name? - In which cases would it be useful to have multiple remote repositories?
Make changes in the packing_list.md
file on the main
branch on the remote repository directly in the browser.
Go to the repository on the remote server and open the packing_list.md
file. Add some items and commit the changes.
You want to include the additional items from the main branch in your feature branch. To do this, you need to merge the feature/blankets
branch into the main
branch.
On your local machine, switch to the main
branch and pull the latest changes from the remote repository.
git checkout main
git pull origin main
Before you continue, think about the following questions:
- What is the purpose of the
git pull
command? - What is the difference between
git pull
andgit fetch
? - What would happen if you tried to push changes to a branch that has new commits on the remote repository? Try it out!
- What is the difference between
git pull origin main
andgit pull
?
Merge the feature/blankets
branch into the main
branch.
git merge feature/blankets
- How could you prevent a merge commit from being created when merging branches? Why would you want to do that?
- What would have been the result if you had merged the
main
branch into thefeature/blankets
branch instead?
Open the packing_list.md
file and resolve the merge conflict.
-
The file will contain conflict markers
<<<<<<<
,=======
, and>>>>>>>
to indicate the conflicting changes. For example when Bob and Alice both added items to the same line in thepacking_list.md
file. Bob added "Extra blankets" directly on the main branch, and Alice added "Sleeping bags" on thefeature/blankets
branch. The file might look like this:Blankets <<<<<<< HEAD - Extra blankets ======= - Sleeping bags >>>>>>> feature/blankets
-
Edit the file to keep the changes you want to include. In this case, you can keep both items.
Blankets - Extra blankets - Sleeping bags
-
Save the file and commit the changes.
git add packing_list.md git commit -m "Resolve merge conflict"
Note: You can also use a merge tool to resolve the conflict. E.g.: git mergetool
or in IDEs like Visual Studio Code, you can use the built-in merge tool to resolve the conflict. It will show you the changes from both branches side-by-side, and you can choose which changes to keep.
Push the changes to the remote repository.
git push origin main
Now the feature/blankets
branch is merged into the main
branch, and the conflict are resolved. The packing list is updated with the items from both branches.
- Which strategy would you have had to use to merge the feature branch into the main branch without creating a merge conflict?
- Is avoiding a merge conflicts generally a good strategy?
Now that you've set up the repository and added items locally, it's time to step through a feature branch development cycle using the project management tools provided by the remote services. This part will focus on working entirely online in the remote IDE.
Carol, the tech enthusiast, wants to add a drone to the packing list. She will start by creating a project board and adding tasks/issues for the items she wants to bring.
- Go to the repository on the remote server: Weekend Out
- Fork the repository to your account.
- Create a new project board called "Drone Feature".
- Add tasks/issues to the project board:
- Task 1: Add drone to packing list.
- Task 2: Add extra batteries to packing list.
- Task 3: Add camera to packing list.
- Task 4: Review changes and resolve conflicts.
Carol will create a new feature branch directly on the remote repository.
- Go to your forked repository on the remote server (GitHub or GitLab).
- Create a new branch called
feature/drone
from the main branch.
Carol will add her items to the packing_list.md
file directly in the browser.
- Navigate to the
packing_list.md
file in thefeature/drone
branch in the remote repository. Open the file for editing in the remote IDE. - Add the first item that Carol wants to bring (e.g., drone).
- Commit this first added item with a descriptive message. Link the commit to the respective task on the project board. Hint: When you start typing
#
, you will see a list of tasks from the project board to link to the commit.- Why is it useful to link commits to tasks on the project board?
Carol will use the project management tools to track her progress.
- After completing the first task, move it manually on the project board as she completed it. For the other tasks, Carol will use automatic project board updates.
- Think about how you can track the progress of a project by automatically updating the project board based on commit messages?
- Add the other items to the
packing_list.md
file and commit them with descriptive messages. Link the commits to the respective tasks on the project board.
Once Carol has added all the items to the packing_list.md
file, she will merge her feature/drone
branch into the main
branch.
- Create a pull request from
feature/drone
tomain
. Link the pull request to the tasks on the project board with closing keywords.
While Carol was working on her feature branch, Alice made changes to the main branch that conflict with Carol's changes.
- Resolve the conflicts in the
packing_list.md
file directly in the browser. - Commit the changes with a descriptive message. Remember to link the commit to the respective task on the project board.
After resolving the conflicts, Carol will request a review of her changes to the packing list from another team member.
- Request a review from another team member on the pull request.
- Start the review process by adding comments to specific lines in the
packing_list.md
file.- Propose changes to some lines in the
packing_list.md
file. - Accept the changes and approve the pull request.
- Propose changes to some lines in the
- Merge the pull request and delete the
feature/drone
branch. - Check the project board to see the status of the tasks. They should all be marked as "Done".
- Discuss the benefits of using feature branches and project management tools.
- Reflect on the experience of resolving conflicts online.
- Consider how these practices can improve collaboration and project management in a research setting.