Intro to git

Block 2.3: Branches & Merging

Jan Simson

Alternate Realities:
git branches

Background: https://unsplash.com/photos/V1YEEItoOTE

What are git branches? 🌳

  • Branches allow you to have different versions of your code within the same repository next to each other
  • You can switch back and forth between branches
  • In the end you can merge your changes back into your main branch

Note

The default branch used to be called master, this was more-or-less recently changed to main, but not all repositories have changed yet.

What are git branches? 🌲

Example of a repository history with branching

Creating new branches

  • There are two ways to create new branches
  • git checkout -b my-branch
    • Create a new branch and immediately check it out
  • git branch my-branch
    • Create a new branch, but don’t check it out
git checkout -b my-branch
# Switched to a new branch 'my-branch'

Working on branches

  • So far you have always been working on a single branch: main
  • Nothing changes when working with multiple branches, you just have to keep an eye on your current branch
git status
# On branch my-branch
# nothing to commit, working tree clean

Switching branches

You can switch branches with git checkout (as we’ve already done earlier)

git checkout my-branch

Tip

There’s no more -b when the branch already exists

Note

You can also use git switch my-branch, but this command is still experimental right now and may change.

Bringig it all together: Merging

  • You can combine two different branches by merging them with each other
  • git merge my-branch
    • You will merge the branch that you name in the command into the one you are currently on
    • In most cases you will want to be on the main branch when using git merge (but not always!)
  • Merging will create a new “merge” commit

Bringig it all together: Merging

Note

Only branches with a shared history can be merged, but this should (almost) always be the case.

Visual Example of Merging

Example of a merged repository history

Demo: learngitbranching.js.org

Background: https://giphy.com/gifs/berlin-demo-govid-loAFGkJxYzgxu3NKnZ

Practical: Branches and merging

  1. Go back to your Terminal in git-exercise
  2. Create a new branch called new-feature
  • Make sure to also checkout that branch, you can check whether you’re on it with git status
  1. Add a commit on that new branch
  2. Go back to main
  3. Merge the new-feature branch into main
  4. Repeat all the above steps (with a different branch name & commit) using a GUI

The Ugly Side of git: Merge Conflicts

What are Merge Conflicts?

  • git is very smart in the way it combines changes from two branches
    • But there’s not always a clear solution
  • If git doesn’t know how to merge the two branches, we get a merge conflict
    • Merge conflicts have to be manually resolved (by us)

How to Get a Merge Conflict 🧨

And how to avoid it.

  • Merge conflcits occur when there are edits to the same file (and at the same location) on two different branches
  • If you merge your branches / edits before making more changes, you can avoid conflicts

Merge Conflicts: Illustration (1)

Starting situation, each box corresponds to the file’s contents in a commit

Merge Conflicts: Illustration (2)

The merge conflict arises because both change the same line

Merge Conflicts: Illustration (3)

We could’ve avoided the merge conflict, by merging main into feature before doing the change on feature

Resolving Merge Conflicts

  • To resolve a merge conflict, we will have to pick one of the two versions
    • Once a solution is picked for every conflict you can commit the solution and the merge continues / finishes
  • Picking a solution is easiest to do by using GUI tools
    • Both Sourcetree and VSCode have great UIs for this

DEMO: Resolving merge conflicts in VS Code and SourceTree

Practical: Creating & Resolving a Merge Conflict

Practical: Setup

  1. Create a new file called guessing_conflict.txt
  2. Write the “Question” with the letters A to D into the file, with empty lines in between e.g.
Question A

Question B

Question C

Question D

Practical: Setup

  1. Create a new branch called guesses and check it out
    • Double check whether you checked out the correct branch with git status
  2. We’re done with the preparations

Practical: Guessing Game

Write your guess below the line with the matching letter.

  1. How many liters of Beer have been drunken at the Oktoberfest in Munich in 2019? 🍻
  2. How many people live in Karlsruhe? (Exactly) 🏘️
  3. What percentage of Americans asked in a survey in 2017, indicated that they believe in Astrology? 🌟
  4. What percentage (same sample as C), indicated that they believe in psychics? 🔮

Practical: Guessing Game Answers

  1. Check out the main branch again
  2. Write down the actual answers below the letters in the now empty file again
Question A 🍻 Liters of beer [^1]
7,850,200
Question B 🏘️ People in Karlsruhe
313,092
Question C 🌟 % believing in Astrology [^2]
29%
Question D 🔮 % believing in Psychics [^2]
41%

Practical: The Actual Conflcit

  1. Use git merge to merge the guesses branch into your main branch
  2. Explore the files in the resulting merge conflict
  3. Resolve the merge conflict

🎉 Bonus Practical: learngitbranching

  1. Go to https://learngitbranching.js.org/
  2. Finish Lessons 1, 2 and 3 on the website

🎉 Bonus: Rebasing - The Alternative to Merging

  • You can also combine two branches by rebasing them (instead of merging)
  • When rebasing, all extra commits on your current branch will be moved to the target branch
    • This alters the affected commits ‼️
    • Leads to a single history
  • Use with caution
# You call git rebase e.g. on the feature branch
git checkout feature

🎉 Rebasing: The Alternative to Merging

Merging

Rebasing

End of Block 🎉

Any Questions?

[🏡 Back to Overview]

[⏩️ Next Block]