Git & GitHub ยท Chapter 14 of 42

Understanding Branches

A branch in Git is an independent line of development. The default branch is usually called `main` (or `master` in older repos). Branches let you work on new features or fixes without affecting the main codebase.

Branches in Git are lightweight and fast to create because they're just pointers to a specific commit, not full copies of the project. This encourages frequent branching for experiments and features.

Syntax
git branch
git branch -a

Why use branches?

Branches isolate work in progress. You can experiment, build a feature, or fix a bug on a branch, and only merge it into main once it's tested and ready.

Listing branches

Run `git branch` to see all local branches, with an asterisk marking the branch you're currently on. Use `git branch -a` to also see remote branches.

Example 1 (bash)
git branch
Output
* main
  feature-login

Lists local branches; the asterisk shows main is the currently checked-out branch.

Example 2 (bash)
git branch -a
Output
* main
  feature-login
  remotes/origin/main

Shows both local branches and remote-tracking branches.

Key points

  • A branch is an independent line of development.
  • The default branch is usually called main.
  • Branches are lightweight pointers to commits, not full copies.
  • git branch lists existing branches; the asterisk shows the current one.
๐Ÿ’ก Note: Frequent branching for features and fixes is a core part of a healthy Git workflow.

๐Ÿ“ Quick Quiz

1. What is a Git branch?

2. What is the usual name of the default branch?

3. What does the asterisk (*) mean in git branch output?