Branches and merging
Work on features in isolation and bring them back together.
A branch is a movable pointer to a line of commits. It lets you try something without touching the stable code. Creating a branch is instant and cheap.
git branch # list branches
git switch -c login-page # create and move to a new branch
# ...edit, git add, git commit...
git switch main # go back to main
git merge login-page # bring the work into main
git branch -d login-page # delete the finished branchMerge conflicts
When two branches change the same lines, Git cannot choose and marks the file:
<<<<<<< HEAD
title = "Welcome"
=======
title = "Hello"
>>>>>>> login-page- Open the file and keep the text you want, deleting the marker lines.
- Run
git addon the file. - Run
git committo finish the merge.
NoteSmall branches that live a few days produce small conflicts. Long-lived branches produce painful ones.
Older command
git checkout -b name still works and does the same as git switch -c name. You will see both in tutorials.
Test yourself
Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.