Web Development · Intermediate level

Intermediate level · Module 2: Collaborating with Git and GitHub · Lesson 1 of 3

Branches and a Clean History

Create branches for each piece of work, write useful commits and read history and diffs.

In this lesson you will

  • Create, switch and merge branches
  • Write meaningful commits and inspect history

Key concepts

branchgit switchgit diff and git logcommit messages

The lesson

A branch is a separate line of work. git switch -c add-gallery creates and moves to a new branch; changes there do not affect main until they are merged. Use one branch per feature or fix.

Commit in small steps with messages that explain why: 'Add lazy loading to gallery images to reduce data use'. git diff shows unstaged changes, git log --oneline shows history.

Keep main always working. Before merging a branch, update it from main and check that the site still works.

Step by step

  1. Create a feature branch and make two commits.
  2. Inspect the changes with git diff and git log.
  3. Switch back to main and see the changes disappear.
  4. Merge the branch into main.

Practice activity

Develop one feature of the team site on its own branch with at least three clear commits.

Expected result: A branch with focused commits, merged cleanly into a working main branch.

Check your understanding

  1. Why use a branch for each feature?

    Show answer

    Work is isolated until it is ready, so main stays working.

  2. What should a commit message explain?

    Show answer

    What changed and why.

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.