Branches
100%

Git · Lesson 5

Branches

Learn how Git branches identify different lines of work and how a merge brings a change into main.

A branch is a name that points to a commit. When you make a new commit on your current branch, its name advances to the new entry. Another branch keeps its own position until you update it.

Starting a Separate Line of Work

From main in the practice repository, create and switch to a branch named health-page. The -c option on git switch means create.

$ git switch -c health-page

Both names point to the same commit at first. Use git branch --show-current to check which name your next commit will advance.

Right after git switch -c health-page, which commit does the new name point to?

Saving a Change on the Branch

Add a one-line status file and commit it while health-page is current.

$ printf '%s\n' ready > health.txt
$ git add health.txt
$ git commit -m "Add a health page"

Imagine the homepage commit is A and the health page commit is B.

A (main) --- B (health-page)

Only health-page moved to B. Switching back to main with a clean working tree returns your files to the snapshot at A.

Before merging, which branch contains the health page commit?

Merging into Main

Switch to the branch that should receive the change, then name the branch you want to merge from.

$ git switch main
$ git merge health-page

Here main has no changes of its own after A. Git can move it directly to B, an operation called a fast-forward. If the two branches each have new commits, Git may have to combine their changes. Read the result before assuming the merge finished.

You want main to receive health-page's change. Where do you run the merge?

Lesson complete

You finished Branches

You can now follow a change from a local branch into main.

  • Create a branch at the current commit.

  • Save a change on the intended branch.

  • Merge the change into the branch that should receive it.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Next Lesson
Back to Git