Learn / Programming / Git & Command Line / Branching and Merging

Git & Command Line · Lesson 3 of 5

Branching and Merging

Work in parallel, merge, resolve conflicts and rebase.

  • Intermediate
  • 16 min read
  • 3 objectives

Before this lessonLesson 2: Git Basics

What you will learn

  • Create and switch branches
  • Resolve a merge conflict
  • Choose merge vs rebase

A branch is an independent line of development. Branches let you build a feature or fix a bug without disturbing the stable code, then combine the work when it is ready. In Git, branches are extremely cheap: just a movable pointer to a commit.

Branches let you experiment safely

Imagine you want to try a risky redesign but the site must keep working for visitors. A branch is a separate line of development: you copy the current state, work on your copy, and if it goes well you merge it back. If it goes badly you delete the branch and nothing was harmed. Branches are cheap in Git (just a pointer), so professional teams create one for every feature and bug fix.

main:     A---B---C
               \
feature:        D---E     <- your experiment, isolated from main

Working with branches

git branch                     # list branches
git switch -c feature/login    # create AND switch to a new branch
# ...edit, commit...
git switch main                # go back
git branch -d feature/login    # delete a merged branch

HEAD is Git's name for "where you are now". Committing moves the current branch forward.

Merging

git switch main
git merge feature/login
  • Fast-forward: if main has not moved, Git just slides its pointer forward.
  • Merge commit: if both branches have new commits, Git creates a commit with two parents that joins them.

Merge conflicts

A conflict occurs when both branches changed the same lines. Git stops and marks the file:

<<<<<<< HEAD
const title = "Welcome";
=======
const title = "Hello there";
>>>>>>> feature/login

To resolve: open the file, choose or combine the correct code, delete the three marker lines, then finish.

git add src/app.js
git commit            # completes the merge
# changed your mind?  git merge --abort

Rebase

git rebase main replays your branch's commits on top of the latest main, producing a straight, linear history instead of a merge commit.

git switch feature/login
git rebase main          # re-apply my commits on top of main
git switch main
git merge feature/login  # now a clean fast-forward

Naming and habits

  • Prefix names by intent: feature/, fix/, chore/.
  • Keep branches short-lived: days, not months.
  • Keep main always working.

A full feature workflow

$ git switch -c add-search        # create a branch and move to it
Switched to a new branch 'add-search'

$ # ...edit files...
$ git add .
$ git commit -m "Add search box"

$ git switch main               # go back to main
$ git merge add-search          # bring the feature in
Updating 3f2a91c..9d1e4b7
Fast-forward
 search.html | 12 ++++++++++++

$ git branch -d add-search      # tidy up, the work is now on main

Fast-forward versus merge commit

If main has not moved since you branched, Git simply slides the pointer forward: a fast-forward, no extra commit. If main also gained new commits, Git creates a merge commit that joins the two lines and has two parents.

Conflicts are normal, not a disaster

A conflict happens only when two branches change the same lines of the same file, and Git cannot decide which to keep. It stops and marks the file. Your job is to choose, then finish the merge.

<<<<<<< HEAD
<h1>Welcome to our shop</h1>
=======
<h1>Welcome to the store</h1>
>>>>>>> add-search
  • Everything between <<<<<<< and ======= is what is on your current branch.
  • Everything between ======= and >>>>>>> is what the other branch has.
  • Edit the file to the version you want, deleting all three marker lines.
  • Then git add file and git commit to complete the merge.

Key takeaways

  • A branch is an isolated line of work; create one per feature or fix.
  • git switch -c name creates and enters a branch; git merge name combines it.
  • Conflicts only occur when the same lines changed; you resolve them by editing and committing.
  • git merge --abort cancels a merge you are not ready to finish.
# Write your solution here
Up next · Lesson 4Remotes, GitHub and Pull RequestsPush, pull, fork and review code with pull requests.