Lesson

Git Branch

10 min reading
Free Course

Git Branch: Developing Features in Isolation

A Git branch is a lightweight movable pointer to a specific commit, allowing you to build new features or test bug fixes without disturbing the stable main branch. Creating a branch takes milliseconds because Git only writes a tiny pointer file rather than copying your whole project folder.

How Branching Keeps main Safe

Instead of duplicating project directories manually, Git lets multiple lines of work progress in parallel and merge cleanly when tested.

gitGraph
   commit id: "Initial Commit"
   commit id: "Add styles.css"
   branch feature/header-nav
   checkout feature/header-nav
   commit id: "Build Header Markup"
   commit id: "Add banner.svg"
   checkout main
   commit id: "Hotfix Footer Link"
   merge feature/header-nav

Creating, Listing, and Switching Branches

List existing local branches with git branch, where the asterisk (*) marks your active branch. Create and switch to a new branch in one step with git switch -c (or the older git checkout -b):

git branch
git switch -c feature/header-nav

Now suppose you add assets/banner.svg and update index.html and styles.css on feature/header-nav:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Starter Site</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <header class="site-header">
    <h2>Starter Site Navigation</h2>
    <object data="assets/banner.svg" type="image/svg+xml"></object>
    <p>Header navigation updated on feature branch.</p>
  </header>
</body>
</html>

Stage and commit those updates on your feature branch:

git add index.html assets/banner.svg
git commit -m "Add header navigation and banner.svg"

If an urgent bug appears on main, switch back cleanly with git switch main. Notice that assets/banner.svg disappears from your working folder while you are on main, then reappears the moment you switch back to feature/header-nav.

Renaming and Deleting Branches

Rename the current branch or delete a branch once its commits are merged into main:

git branch -m feature/top-nav
git branch -d feature/top-nav

Use -D (uppercase) only when you intentionally want to discard an unmerged experimental branch.

Common Branching Mistakes

Starting feature work directly on main and realizing five commits later you needed a separate branch is common. Fix it before pushing by creating the branch at your current commit (git branch feature/header-nav) and resetting main back to origin/main.

Frequently Asked Questions

Should I use git switch or git checkout?

Use git switch for changing or creating branches and git restore for discarding file changes. git checkout still works, though newer Git splits those two jobs into clearer commands.

What does a branch name like feature/header-nav mean?

The slash is just a naming convention teams use to organize branches by category, such as feature/, bugfix/, or hotfix/. Git treats it as a standard branch name.

Can I delete the branch I am currently standing on?

No. Switch to another branch such as main first, then run git branch -d <branch-name>.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol
Git Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum