Git/
Lesson

Git branches create parallel timelines where you can experiment, build features, or fix bugs without touching your stable, production-ready code.

What exactly is a branch?

A branch is a movable pointer to a specific commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed.. When you create a branch, Git doesn't copy all your files. It creates a new pointer at your current position. As you make commits on that branch, the pointer moves forward, while other branches stay where they are.

main:     A---B---C---F---G (production-ready code)
               \         /
feature:        D---E---/   (experimental feature)

Here, feature diverged after commit B, did its own work (commits D and E), then merged back into main at commit G. That's parallel development that converges when ready.

Under the hood
A branch is literally just a file containing a 40-character SHA-1 hash pointing to the latest commit on that branch. When you switch branches, Git updates your working directory to match that commit's snapshot. No copying, no duplicating, just efficient pointer management.
02

Why you need branches

Working directly on main is like performing live edits on a website that thousands of people are viewing. Branches give you a safe sandbox.

Isolation

On a feature branch, you can break things, rewrite everything, or change your approach. The main branch remains untouched. If your experiment fails, delete the branch, no harm done.

Collaboration without chaos

With branches, each developer works on their own feature branch:

main:        A---B---C---H---I (stable)
                  /   /   /
alice:         D---E   /     (user auth)
bob:                 F---G   (payment system)

Neither interferes with the other, and main stays deployable throughout.

AI pitfall
AI tools sometimes suggest committing directly to main, "just run git add . && git commit && git push" without mentioning branches. In any real project, always create a feature branch first. Direct commits to main bypass code review and can break production.

Experimentation and code review

Branches let you try wild ideas risk-free, create experiment/complete-refactor, and if it fails, just delete the branch. They also enable pull requests, where others review your code before it enters main, catching bugs and improving quality.

03

The default branch: your source of truth

Every Git repositoryWhat is repository?A project folder tracked by Git that stores your files along with the complete history of every change, inside a hidden .git directory. starts with a default branch. Historically called master, the modern convention is main. This branch represents your official, stable codebase. In professional environments, main is typically protected, all changes must come through reviewed pull requests.

Branch nameCommon usageWhen to use it
mainProduction-ready codeThe default, always stable
masterLegacy projectsOlder repositories
developIntegration branchGit Flow workflow
feature/*New featuresWhen building something new
bugfix/*Bug fixesWhen fixing issues
hotfix/*Critical production fixesEmergency patches

Check which branch you're on with git branch, the asterisk (*) marks your current active branch.

04

The branch workflow

The standard workflow used by virtually every development team:

Step 1: Create your branch

git branch feature/user-login

Step 2: Switch to your branch

Creating a branch doesn't switch you to it. You need to check it out:

git checkout feature/user-login
Pro tip
Combine creation and switching with git checkout -b feature/user-login. More on this in the next lesson.

Step 3: Work and commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed.

Make changes, stage them, and commit. These commits only exist on this branch, main remains unchanged.

Step 4: Switch back and merge

When your feature is complete:

git checkout main
git merge feature/user-login

Your feature is now part of the official codebase.

05

Best practices

Keep main sacred. Never commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. work-in-progress to main. It should always be deployable.

One branch, one purpose. Don't build three features on the same branch. Smaller branches are easier to review, test, and merge.

Name branches descriptively. Use prefixes and clear descriptions: feature/user-authentication, bugfix/login-error-502, hotfix/security-patch-cve-2024.

Delete merged branches. Once merged, a branch is just clutter:

git branch -d feature/old-branch        # Delete locally
git push origin --delete feature/old-branch  # Delete remotely

Pull before you branch. Always update main before creating a new branch:

git checkout main
git pull origin main
git checkout -b feature/new-feature
Good to know
Some teams use more complex branching strategies like Git Flow (with develop, release, and hotfix branches) or GitHub Flow (feature branches only). Master these basics first, and advanced workflows will feel natural.
06

Quick reference

CommandWhat it doesWhen to use it
git branchList all branchesSee what exists
git branch nameCreate a branchStart new work
git checkout nameSwitch to a branchMove between work
git checkout -b nameCreate and switchMost common shortcut
git branch -d nameDelete a merged branchClean up after merge
javascript
# List all branches
git branch

# List all branches including remote ones
git branch -a

# See which branch you're on (marked with *)
# main
# * feature-login    ← you're here
# feature-signup