GitIntroduction to Branches

Introduction to Branches

Branches are Git’s killer feature. They let you work on multiple things at once — features, fixes, experiments — without those work streams interfering with each other. Branches in Git are so cheap and so fast that experienced developers create them constantly. Understanding what a branch really is makes everything that follows simpler.

What a branch actually is

A branch is a movable pointer to a commit. That is the entire definition. It is not a copy of the files, not a snapshot, not a folder. It is a 41-byte file inside .git/refs/heads/ that holds one commit hash.

Proof that a branch is just a pointer

Bash
cat .git/refs/heads/main
# 1f9ab2c0e8b4a7d2f0c...   (a single commit hash)

Because branches are pointers, creating one is instantaneous and deleting one is harmless. The commits do not disappear — the pointer just goes away.

HEAD — the “you are here” pointer

HEAD is a special pointer that says which branch you are currently on. When you make a commit, Git follows HEAD to learn which branch to advance.

The picture

Text
HEAD ──▶ refs/heads/main ──▶ commit C

    A───B───C   ◀── main (HEAD is here)
            \
             D───E   ◀── feature-x

After 'git switch feature-x':
HEAD ──▶ refs/heads/feature-x ──▶ commit E
Why branches are useful
  • Parallel work — keep main always stable, do new work on branches.

  • Safe experiments — try a wild refactor, throw it away if it does not work; main is untouched.

  • Code review — propose a branch as a Pull Request so others can review before it lands.

  • Bug fixes alongside features — fix something urgent without disrupting the feature you are mid-way through.

  • Release engineering — keep a release/1.x branch alive for patches while main charges ahead.

The basic branch lifecycle

From idea to merged branch

Bash
# 1. Start from a clean main
git switch main
git pull

# 2. Create a branch for the work
git switch -c add-search-feature

# 3. Make commits on the branch
git add .
git commit -m "Add search component"
# (more edits, more commits)

# 4. Push the branch so others can see it
git push -u origin add-search-feature

# 5. Open a Pull Request on GitHub, get review, merge into main

# 6. Clean up locally
git switch main
git pull
git branch -d add-search-feature
Branches and the commit graph

A typical small graph

Text
           A───B───C   ◀── main
                    \
                     D───E   ◀── feature

A, B, C are the commits on main.
D was created when you branched off C and made a commit.
E is one more commit on feature.

When you merge feature back into main, you typically get a merge commit:

After git merge feature

Text
           A───B───C───────M   ◀── main
                    \     /
                     D───E       ◀── feature (still exists if you don't delete)
Default branch names
  • main — modern default. Adopted by GitHub in 2020 and now the industry standard.

  • master — older Git default, still common in legacy repos.

  • develop — used in the Gitflow workflow as the integration branch.

  • trunk — used in trunk-based development (rare).

Renaming master → main
On older repos you may see `master`. You can rename locally with `git branch -m master main`, push the new name, and update the default branch in your hosting provider’s settings.
Why branching is cheap in Git
  • A branch is just a 41-byte file. Creating one is instantaneous.

  • No file copying happens — your working directory is unchanged when you create a branch.

  • Switching branches updates the working directory to match the new branch’s commit. On modern SSDs this is sub-second even for huge repos.

  • You can have hundreds of branches in a repo without any performance impact.

Commands you’ll use every day

The essential branch toolkit

Bash
git branch                       # list local branches
git branch -a                    # list all branches incl. remote
git switch -c new-branch         # create AND switch
git switch existing-branch       # switch to an existing branch
git branch -d done-branch        # delete a merged branch
git branch -D abandoned-branch   # force-delete (even if unmerged)
git branch -m new-name           # rename current branch
git merge feature                # merge another branch INTO the current one
Tip
A useful rule of thumb: one branch per logical unit of work. One feature, one bug fix, one refactor → one branch. Short-lived branches (hours, a couple of days) are easier to merge and review than long-lived ones.