appzasGamesToolsDevLearnCheat sheetsQuick calcsConvertersReferenceNetworkTimeCalculatorsCompareLLM prices

Branches and merging

Lesson 2 of 410 minBeginner

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 branch

Merge conflicts

When two branches change the same lines, Git cannot choose and marks the file:

<<<<<<< HEAD
title = "Welcome"
=======
title = "Hello"
>>>>>>> login-page
  1. Open the file and keep the text you want, deleting the marker lines.
  2. Run git add on the file.
  3. Run git commit to 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.

Hintgit switch with the create flag.
Hintgit merge and the branch name.
3. What causes a merge conflict?
4. After fixing the conflict markers in a file, what is the next step?

Key terms