Branches and merges

What a branch is

Understand a branch as a movable name that points to the latest commit in one line of development.

8 minute lesson

~~~

A branch is a readable name that points to a commit.

Suppose main points to commit C:

A---B---C  main

Creating a branch named add-search creates another reference to the same commit:

A---B---C  main, add-search

No files are copied. A branch is lightweight because Git only needs another reference.

HEAD normally points to the branch you currently have checked out. If HEAD points to add-search and you commit, Git creates a child of C and moves only add-search:

A---B---C  main
         \
          D  add-search <- HEAD

The main reference remains at C. The commits are not “inside” folders called branches. References give Git starting points for walking the commit graph.

Inspect the current branch with:

git branch --show-current
git log --oneline --decorate --graph --all

Exercise: draw three commits and two branch references. Move HEAD to one branch, add a commit, and predict which reference moves.

Lesson completed

Take this course offline

Get every free book, course edition, and software download.

Get the download library →