Git & Version Control Fundamentals

Lesson 3 of 5

Branching and Merging

A branch is an independent line of development. The default branch is usually called main.

Creating and switching branches

git branch feature-login        # create a branch
git switch feature-login         # switch to it

# or do both in one step:
git switch -c feature-login

Older tutorials use git checkout -b feature-login, it does the same thing as git switch -c.

Merging

Once your branch is ready, merge it back into main:

git switch main
git merge feature-login

Merge conflicts

If the same lines were changed on both branches, Git can't automatically decide which version to keep, that's a merge conflict. Git marks the conflicting lines directly in the file:

<<<<<<< HEAD
const greeting = "Hello!";
=======
const greeting = "Hi there!";
>>>>>>> feature-login

Edit the file to keep the version you want, remove the <<<<<<</=======/>>>>>>> markers, then stage and commit as usual.

WARNING

Always pull the latest changes before starting new work, resolving a conflict is much easier when it's small.

📝 Branching Quiz

Passing score: 70%
  1. 1.Which command creates and switches to a new branch in one step?

  2. 2.Combining changes from one branch into another is called a ____.

  3. 3.A merge conflict happens when Git can automatically combine changes without any help.