Session 5: Branches

Track, organize and share your work: An introduction to Git for research
Course at Max Planck Institute for Human Development
Slides | Source
License: CC BY 4.0 DOI

12:00

1 This session: Branches

This session: Branches

Learning objectives

💡 You understand the purpose and benefits of using branches in Git.
💡 You can create and switch between branches.
💡 You can merge branches and resolve merge conflicts.
💡 You can name at least three best practices when working with branches.

Reading

“Branches”

Tasks

In this session, you will work on the following tasks:

  1. Reading: Read the chapter(s) “Branches” in the Version Control Book.
  2. Implementation: Try out the commands in the chapter.
  3. Exercises: Work on the exercises for the recipes project.

As always:

  1. Try out the commands of this session and play around with them.
  2. Check whether you have achieved the learning objectives.
  3. Ask questions!
  4. Let’s git started!

recipes project

At the end of this session, you should have accomplished the following:

  1. You created a new branch and merged changes into your default branch (main or master).
  2. You created and resolved a merge conflict.

Please keep the recipes folder! We will continue to use it in the following sessions.

Cheatsheet

Command Description
git branch Lists / creates and deletes branches
git branch feature Creates the feature branch
git branch -d feature Deletes the feature branch
git switch Switches between branches
git switch feature Switches to the feature branch
git checkout Switches between branches
git checkout -b feature Creates and switches to the feature branch
git merge Merges branches
git merge feature Merges the feature branch into the current branch
git merge --abort Aborts a merge
git merge --squash Squaches commits on branch into a single commit and merge

Exercises

Branches

  1. If needed, navigate to the project repository using the command line.
  2. Create a new branch with a descriptive name, e.g., add-pizza-recipe.
  3. Switch to the new branch.
  4. Add a new recipe to recipes.txt.
  5. Stage and commit the changes to recipes.txt on the new branch.
  6. View the contents of recipes.txt to verify your changes.
  7. Switch back to the default branch (main or master).
  8. View the contents of recipes.txt again to confirm that the changes do not exist on the default branch.
  9. Merge the new branch into your default branch.
  10. Delete the feature branch.
  11. View the contents of recipes.txt yet again to confirm that the changes have been merged into the default branch.

🚀 Bonus exercises

Create and resolve a merge conflict

  1. Deliberately create a merge conflict by making conflicting changes to the same section of recipes.txt on two separate branches (for example, change the same ingredient differently on each branch) and attempting to merge them. An example can be found in the branches chapter.
  2. Resolve the merge conflict.
  3. Delete the merged branch afterwards.

Solutions: Branches

cd recipes
git branch feature
git switch feature
cat >> recipes.txt <<- EOM

Potatoes

1. Wash the potatoes and cut them into evenly sized chunks.
2. Bring water to the boil in a large saucepan.
3. Once the water is boiling, add a good pinch of salt.
3. Add the potatoes.
4. Cook the potatoes for 15 - 20 minutes or until fork-tender.
5. Drain the pasta in a colander.
EOM
git add recipes.txt
git commit -m "Add potatoes recipe to recipes.txt"
cat recipes.txt
git checkout main
cat recipes.txt
git merge feature
git branch -d feature
cat recipes.txt
  1. Optional: Navigate into the project repository using cd (or a similar path).
  2. Create a new branch called add-pizza-recipe using git branch add-pizza-recipe.
  3. Switch to the new branch using git switch add-pizza-recipe. You can also create and switch the branch in one step using git checkout -b add-pizza-recipe.
  4. Add a new recipe to recipes.txt. You can use your regular text editor. Here, we add a new entry from the command line using cat.
  5. Stage and commit the changes to recipes.txt using git add and git commit.
  6. View the contents of recipes.txt to verify your changes. Here, we use the cat command again.
  7. Switch back to the default branch (main in this example). Here, we use git checkout main but you can also use git switch main.
  8. View the contents of recipes.txt again to confirm that the previous changes do not exist on the main branch.
  9. Merge the add-pizza-recipe branch into the main branch.
  10. Delete the merged add-pizza-recipe branch using git branch -d add-pizza-recipe.
  11. View the contents of recipes.txt yet again to confirm that the previous changes have been merged into the main branch.

Solutions: Merge Conflict

cd recipes
git branch feature
git switch feature
cat >> recipes.txt <<- EOM

Chocolate Cake

1. Preheat the oven to 350°F (175°C).
2. Mix flour, sugar, cocoa powder, baking powder, and salt in a bowl.
3. Add eggs, milk, oil, and vanilla extract, and mix well.
4. Pour the batter into a greased baking pan.
5. Bake for 30-35 minutes.

EOM
git add recipes.txt
git commit -m "Add chocolate cake recipe to recipes.txt"
git checkout main
cat >> recipes.txt <<- EOM

Vanilla Cake

1. Preheat the oven to 350°F (175°C).
2. Mix flour, sugar, baking powder, and salt in a bowl.
3. Add eggs, milk, oil, and vanilla extract, and mix well.
4. Pour the batter into a greased baking pan. 
5. Bake for 25-30 minutes.

EOM

git add recipes.txt
git commit -m "Add vanilla cake recipe to recipes.txt"
git merge feature
sed -i '' -e '/^<<<<<<< /d' -e '/^=======/d' -e '/^>>>>>>> /d' recipes.txt
git add recipes.txt
git commit -m "Resolve merge conflict by adding both chocolate and vanilla cake recipes"
git branch -d feature
  1. Navigate into the recipes repository using cd (or a similar path).
  2. Create a new branch called feature using git branch feature.
  3. Switch to the new branch using git switch feature. You can also create and switch the branch in one step using git checkout -b feature.
  4. Add a new recipe to your recipes.txt file using cat.
  5. Stage and commit the changes to recipes.txt using git add and git commit.
  6. Switch back to the default branch (main in this example) using git checkout main. You can also use git switch main.
  7. Make conflicting changes in the main branch to recipes.txt using cat.
  8. Stage and commit the conflicting changes to recipes.txt using git add and git commit.
  9. Attempt to merge the feature branch with the default branch to create a merge conflict using git merge feature.
  10. Resolve the merge conflict by editing recipes.txt. You can use a regular text editor to do this. In this example, we remove the conflict markers that Git added to recipes.txt using sed which results in keeping both recipes. This is not a recommended way to resolve merge conflicts and we only do it here to resolve the merge conflict without manual intervention. Merge conflicts usually always require manual resolution by the user.
  11. Stage the resolved changes to recipes.txt using git add.
  12. Commit the resolved changes in recipes.txt with a descriptive commit message using git commit.
  13. Delete the merged feature branch using git branch -d feature.