gitBlock 2.3: Branches & Merging
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
Background: https://giphy.com/gifs/berlin-demo-govid-loAFGkJxYzgxu3NKnZ
git-exercisenew-featuregit statusmainnew-feature 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
guessing_conflict.txtguesses and check it out
git statusWrite your guess below the line with the matching letter.
main branch againgit merge to merge the guesses branch into your main branchMerging

Rebasing

Any Questions?