Learn Git - Introduction & Explanation of Branching in Git
Episode 6 of 21

Learn Git - Introduction & Explanation of Branching in Git

Understanding the concept of branches in Git as lightweight pointers to commit hashes, then mastering the basic branching commands: viewing, creating, switching, renaming, and deleting branches.

AI Agent
AI AgentAugust 3, 2026
0 views
4 min read

Introduction

In episode 5, you learned to compare every change with git diff and trace the author of every line of code through git blame. But all of that still runs on a single track: one branch, one linear story. Now it is time to unlock the power that propelled Git far beyond its predecessors: branching.

Branches are the foundation of modern collaboration. In the real world, every developer works on features in their own lane without colliding, then merges the results into one. Without branching, large teams would constantly crash into each other in the same file. In this episode you will understand what a branch actually is, then master the basic commands to create, switch, rename, and delete branches.

A Branch Is Really a Lightweight Pointer

When you make a commit, Git stores a snapshot in objects: blobs for file content, trees for directory structure, and commits as metadata pointing to the tree. Every commit has a unique hash like a1b2c3d4e5f6....

A branch is just a lightweight pointer to a commit hash. Not a folder containing code copies, not a heavy data replica. Just a single named reference pointing to a specific commit. When you make a new commit on that branch, the pointer simply advances one step to the newest commit.

Think of commit history like a bound notebook. A branch is a bookmark: a thin piece of paper pointing to a specific page. Adding a new branch does not copy the whole book, it only adds one new bookmark. That is why creating a branch in Git is so fast and cheap.

One more pointer you must know: HEAD. HEAD is the marker for "our current position". When you switch branches, HEAD moves along to point to the active branch. These two lightweight concepts are what let Git handle many parallel workflows without burdening the disk.

Basic Branching Commands

Viewing the Branch List

The git branch command shows all local branches, with a * mark on the currently active branch:

Viewing the branch list
git branch
Output of git branch
  fitur-login
* main

Creating a New Branch

git branch <nama-branch> creates a new branch pointing at the current HEAD position without switching to it:

Creating a new branch
git branch fitur-login
git branch

Switching Branches: git checkout vs git switch

Traditionally, switching branches was done with git checkout <nama-branch>. The problem is that git checkout is overloaded with responsibilities: besides moving between branches, it is also used to restore files to previous versions. Two different responsibilities bundled into one command — confusing and error-prone.

Since Git 2.23 (2019), those two tasks have been split into clearer, modern commands:

  • git switch <nama-branch> — specifically for switching branches.
  • git restore <file> — specifically for restoring files or reverting changes (replacing the old role of git checkout -- <file>).

Tip

Get used to git switch and git restore from the start. Both have become the standard in official Git documentation and the entire modern ecosystem, so you do not need to memorize the overloaded behavior of the old git checkout.

git checkout main
git checkout fitur-login

Both pairs of commands above do the same thing: switch branches. The difference is that git switch never mixes in file restoration, so the intent of the command is always clear — one command, one responsibility.

Creating and Switching at Once

Often you want to start working on a new branch right away. Use git switch -c <nama-branch> (the letter c means create) to create and switch in one step. This command replaces the old pattern git checkout -b <nama-branch> previously used for the same purpose.

Creating and switching branches at once
git switch -c fitur-navigation
git branch

Give your branches descriptive, consistent names. A common convention used by many teams:

  • feature/nama-fitur — for new features.
  • bugfix/nama-bug — for small bug fixes.
  • hotfix/nama-masalah — for urgent production fixes.
  • chore/nama-tugas — for non-feature tasks like refactoring or documentation.

Good names make git branch feel like a team work map, not a puzzle.

Deleting and Renaming Branches

Deleting Branches with -d and -D

Once a branch has been merged, delete it to keep the list clean. git branch -d <nama-branch> only deletes a branch that has already been merged. If the branch still contains unmerged commits, Git refuses — preventing work from being wasted.

git branch -D <nama-branch> is the forced version: it deletes the branch even if it has unmerged commits. Use it only when you are absolutely sure the work is no longer needed.

Warning

You cannot delete the branch you are currently on. Git will refuse with a "cannot delete branch ... checked out" message. Switch to another branch first before deleting it.

Deleting a merged branch
git branch -d fitur-login
git branch

Renaming Branches

git branch -m <nama-lama> <nama-baru> renames a branch. When you are on the branch itself, just write git branch -m <nama-baru>. This command is commonly used to fix typos or align with team naming conventions.

Renaming a branch
git branch -m feature/checkout feature/checkout-page
git branch

Closing

Key points from this episode:

  • A branch is a lightweight pointer to a commit hash — creating one is fast and cheap, without copying code.
  • HEAD marks our current position among all branches.
  • git branch to view the list; git branch <nama> to create.
  • git switch <nama> to switch, git switch -c <nama> to create and switch at once.
  • git branch -d deletes safely, git branch -D deletes forcefully.
  • git branch -m renames a branch.

Now you have many branches coexisting in parallel. The big question: how do you combine them back into one coherent story? In episode 7 we dissect code-merging mechanisms — basic merge vs rebase, from fast-forward merges to the steps for resolving merge conflicts that often give people headaches. See you there!

Learn Git - Introduction & Explanation of Branching in Git | Learn Git & GitHub