Skip to content
$ lynuxpro
← Git

LESSON 7 OF 21 · INTERMEDIATE · 17 MIN

Branching

HOW IT WORKSA branch is a movable pointer

points toc1Add appc2Add pricingmainbranch
1/4

A branch is just a name pointing at a commit. main points at c2.

Branches let you work on a feature, a bug fix or an experiment in isolation, without disturbing the stable code on main or anyone else's work. In Git, branches are so cheap that teams create one for every change, often several a day. This lesson explains what a branch really is, how to create, switch, list, rename and delete them, and what HEAD means.

In simple words

  • Branch: a separate line of work. You can change things on a branch without touching the main code.
  • main: the main branch, usually holding the working, released code.
  • git switch: moves you to another branch, or creates one with -c.
  • Detached HEAD: looking at an old commit directly, not on any branch. New commits here are easy to lose.

What a branch is

A branch is nothing more than a named pointer to a commit. It is stored as a tiny file containing a 40-character hash. When you commit on a branch, the pointer moves forward to the new commit automatically.

c1 ← c2 ← c3        main → c3
OUTPUTterminalutf-8 · 1L

HEAD is a pointer to the branch you are currently on. That is how Git knows which branch should move when you commit.

c1 ← c2 ← c3        main → c3   ←  HEAD
OUTPUTterminalutf-8 · 1L

Creating a branch just adds another pointer; no files are copied. That is why it is instant even in huge repositories.

Set up

git config --global user.name "Leela Kumar"mkdir branch-labcd branch-labgit initprintf 'def health():\n    return "ok"\n' > app.pygit add app.pygit commit -m "Add health check"
SHELLbashutf-8 · 7L

Create and switch: git switch

git branchgit switch -c feature/cartgit branch
SHELLbashutf-8 · 3L
* mainSwitched to a new branch 'feature/cart'* feature/cart  main
OUTPUTterminalutf-8 · 4L

git switch -c NAME creates a branch at the current commit and switches to it. The * marks the current branch. (The older form git checkout -b NAME does the same; switch was introduced to separate branch operations from file operations.)

Now commit on the new branch:

printf 'def cart():\n    return []\n' > cart.pygit add cart.pygit commit -m "Add empty cart"git log --oneline --all --graph
SHELLbashutf-8 · 4L
[feature/cart 8b2d4e1] Add empty cart 1 file changed, 2 insertions(+) create mode 100644 cart.py* 8b2d4e1 (HEAD -> feature/cart) Add empty cart* 3f9a0c2 (main) Add health check
OUTPUTterminalutf-8 · 5L

feature/cart moved forward; main stayed where it was.

Switching changes your files

When you switch branches, Git updates the working tree to match that branch's latest commit:

lsgit switch mainlsgit switch feature/cartls
SHELLbashutf-8 · 5L
app.py  cart.pySwitched to branch 'main'app.pySwitched to branch 'feature/cart'app.py  cart.py
OUTPUTterminalutf-8 · 5L

cart.py disappeared on main and came back on feature/cart. Nothing was lost; each branch simply shows its own snapshot.

Switching with uncommitted changes

Uncommitted changes travel with you when you switch, as long as they do not conflict with the target branch. If they would be overwritten, Git refuses:

error: Your local changes to the following files would be overwritten by checkout:	app.pyPlease commit your changes or stash them before you switch branches.Aborting
SHELLbashutf-8 · 4L

Commit the work, or put it aside with git stash (lesson 10), then switch.

Diverging branches

Go back to main and commit something different there too:

git switch mainprintf 'def health():\n    return {"status": "ok"}\n' > app.pygit commit -am "Return JSON from health check"git log --oneline --all --graph
SHELLbashutf-8 · 4L
Switched to branch 'main'[main 6c1e7a5] Return JSON from health check 1 file changed, 1 insertion(+), 1 deletion(-)* 6c1e7a5 (HEAD -> main) Return JSON from health check| * 8b2d4e1 (feature/cart) Add empty cart|/* 3f9a0c2 Add health check
OUTPUTterminalutf-8 · 7L

The two branches have diverged: each has a commit the other does not. Combining them is called merging, the subject of the next lesson.

Listing and inspecting branches

git branch -vgit branch --mergedgit branch --no-merged
SHELLbashutf-8 · 3L
  feature/cart    8b2d4e1 Add empty cart* main            6c1e7a5 Return JSON from health check* main  feature/cart
OUTPUTterminalutf-8 · 4L
  • -v shows the latest commit on each branch.
  • --merged lists branches whose work is already in the current branch (safe to delete).
  • --no-merged lists branches with work not yet merged.
  • -a also shows remote-tracking branches like origin/main (next lessons).

Renaming and deleting

git branch -m feature/cart feature/shopping-cartgit branchgit branch -d feature/shopping-cartgit branch -D feature/shopping-cart
SHELLbashutf-8 · 4L
  feature/shopping-cart* mainerror: the branch 'feature/shopping-cart' is not fully mergedhint: If you are sure you want to delete it, run 'git branch -D feature/shopping-cart'Deleted branch feature/shopping-cart (was 8b2d4e1).
OUTPUTterminalutf-8 · 5L

-d refuses to delete a branch with unmerged work, a useful safety net. -D deletes it anyway. Even then, the commits can usually be recovered from the reflog for a while (lesson 15).

Detached HEAD

If you switch to a commit instead of a branch, HEAD points directly at that commit. This is called detached HEAD:

git switch --detach HEAD~1git statusgit switch main
SHELLbashutf-8 · 3L
HEAD is now at 3f9a0c2 Add health checkHEAD detached at 3f9a0c2nothing to commit, working tree cleanSwitched to branch 'main'
OUTPUTterminalutf-8 · 4L

It is useful for looking at old versions. If you make commits while detached and then switch away, those commits are not on any branch. Create a branch first (git switch -c experiment) if you want to keep them.

Naming branches

Teams usually agree on a convention so branches are easy to scan and CI can react to them:

PrefixFor
feature/ or feat/new functionality: feature/cart-discounts
fix/ or bugfix/bug fixes: fix/gst-rounding
hotfix/urgent production fixes
chore/, docs/, refactor/maintenance
ticket numbersfeature/SHOP-142-discounts

Use lowercase and hyphens; avoid spaces and special characters.

Real-world scenario: two urgent requests

Leela is halfway through a discount feature on feature/discounts when a production bug report arrives. She commits her work-in-progress, runs git switch main, git pull, then git switch -c fix/checkout-timeout, fixes the bug and opens a merge request. Twenty minutes later she runs git switch feature/discounts and continues exactly where she was. The bug fix shipped without a single line of unfinished feature code, because each change lived on its own branch.

Common mistakes

  • Committing directly on main when the team works with merge requests. Create a branch first.
  • Long-lived branches that drift far from main and become painful to merge. Keep branches small and short.
  • Forgetting which branch you are on. Check git status or show the branch in your shell prompt (the practice terminal does).
  • Deleting with -D without checking git branch --no-merged.
  • Committing in detached HEAD and losing track of the commits.

Practice

  1. Create a branch feature/login, add a file on it and commit.
  2. Switch back to main and confirm the file is not there.
  3. Make a commit on main and view both branches with git log --oneline --all --graph.
  4. Rename feature/login to feature/sign-in.
  5. Try to delete it with -d and read the message.

Cheat sheet

git branch                       list local branchesgit branch -v | -a | --merged | --no-mergedgit switch -c name [start]       create and switchgit switch name                  switchgit switch -                     back to the previous branchgit switch --detach <commit>     look at an old commitgit branch -m old new            renamegit branch -d name               delete (merged only)git branch -D name               force deletegit log --oneline --all --graph  see all branches
OUTPUTterminalutf-8 · 10L

Key takeaways

  • A branch is a lightweight pointer to a commit; HEAD points to the branch you are on.
  • git switch -c creates a branch; committing moves only the current branch forward.
  • Switching branches updates your files to that branch's snapshot.
  • Branches diverge when both get new commits; merging combines them.
  • Use clear naming conventions and keep branches short-lived.