Advanced git

Block 1.3: Branches, Merging & More

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 we have 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 (usually) 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

Squashing it all together 🤏

You can also squash all commits from another branch together when merging.

git merge --squash <branch>

  • Only a single commit will be added from the merge
  • Loss of history
    • Harder to understand the origin of changes in commit

Squashing it all together 🤏

Merging

Merged (--squash)

Practical: Branches and merging

  1. Go back to your Terminal in git-example
  2. Create a new branch called feature/vegeterian
    • Make sure to also checkout that branch, you can check whether you’re on it with git status
  3. Add a commit (with a new vegetarian recipe) on that new branch
  4. Go back to main
  5. Merge the feature/vegeterian branch into main

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

OPTIONAL DEMO: Resolving merge conflicts in VS Code

Background: https://giphy.com/gifs/iontelevision-fire-chicago-hose-qkTA7B5umJaEwnQ3bW

Practical: Creating & Resolving a Merge Conflict

Practical: Rock-Paper-Scissors 🪨📜✂️

  1. Create a new file called conflict.txt
  2. Write the following lines into your file (including empty lines)
Round 1

Round 2

Round 3

🏁

Practical: Setup

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

Practical: Rock-Paper-Scissors

For each of the three rounds, write your choice of rock 🪨, paper 📜 or scissors ✂️.

Practical: Rock-Paper-Scissors

  1. Check out the main branch again
  2. Add in my choices for each round
Round 1
paper 📜
Round 2
rock 🪨
Round 3
scissors ✂️

Practical: The Actual Conflcit

  1. Use git merge to merge the my-turn branch into your main branch
  2. Explore the files in the resulting merge conflict
  3. For each round, combine the input from both sides as follows and add who won
  4. Resolve the merge conflict
  5. How many points does everyone have?

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
  • Not a remedy for merge conflicts
    • Can actually be more painful to handle

Rebasing: The Alternative to Merging

Starting situation

Rebasing

Rebasing: The Alternative to Merging

Merging

Rebasing

Rebasing to update branches

Rebasing into main 🙅

Rebasing into feature 💁

Questions?

Practical: learngitbranching

  1. Form teams of 2 people each
  2. Open https://learngitbranching.js.org/?NODEMO (the last part is important)
  3. Create a medium complex graph, by playing around with commits, branches, merging and rebasing
  4. Reconstruct the graph created by your partner (and vice versa)

End of Block 🎉

Any Questions?

[🏡 Back to Overview]

[⏩️ Next Block]