gitBlock 1.3: Branches, Merging & More
git branchesBackground: https://unsplash.com/photos/V1YEEItoOTE
main branchNote
The default branch used to be called master, this was more-or-less recently changed to main, but not all repositories have changed yet.
Example of a repository history with branching
git checkout -b my-branch ✅
git branch my-branch ❌
mainYou can switch branches with git checkout (as we’ve already done earlier)
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.
git merge my-branch
main branch when using git merge (but not always!)Note
Only branches with a shared history can be merged, but this should (almost) always be the case.
Example of a merged repository history
You can also squash all commits from another branch together when merging.
git merge --squash <branch>
Merging

Merged (--squash)

git-examplefeature/vegeterian
git statusmainfeature/vegeterian branch into maingit: Merge Conflictsgit is very smart in the way it combines changes from two branches
And how to avoid it.
Starting situation, each box corresponds to the file’s contents in a commit
The merge conflict arises because both change the same line
We could’ve avoided the merge conflict, by merging main into feature before doing the change on feature
Background: https://giphy.com/gifs/iontelevision-fire-chicago-hose-qkTA7B5umJaEwnQ3bW
conflict.txtmy-turn and check it out
git statusFor each of the three rounds, write your choice of rock 🪨, paper 📜 or scissors ✂️.
main branch againgit merge to merge the my-turn branch into your main branchStarting situation

Rebasing
Merging

Rebasing

Rebasing into main 🙅

Rebasing into feature 💁

Any Questions?