Originally published on CodingCush.
A Git branch is a lightweight pointer to a line of commits that lets you work on a new feature, a bug fix or a wild experiment without touching your main, working code. You create a branch, switch to it, commit your changes there, and when everything works you merge it back into main. If the idea fails, you delete the branch and main never knew it existed. This guide covers creating, switching, merging and deleting branches, with real commands you can try today.
What Is a Git Branch?
Think of your project history as a road made of commits. Each commit is a saved snapshot of your files. A branch is a movable signpost on that road. When you commit, your current branch's signpost moves forward to the new commit. The default branch is usually called main, and it holds the stable version you would show to the world.
When you create a branch, Git does not copy your project folder, which surprises many beginners. It simply adds a new signpost pointing at the same commit. That is why Git branches are fast and cheap. You can have ten branches for ten ideas, and your disk will barely notice.
If Git is new on your machine, start with our guide on how to install Git on Windows, Mac and Linux, then follow along here in your terminal.
How to Create a Branch
There are two jobs here: creating a branch, and moving onto it. Plain git branch only creates it. It does not switch you to it, and this tiny detail causes a classic beginner moment: you write code happily, then discover all your commits landed on main. Oops.
# List local branches git branch # Create a branch called feature-login git branch feature-login # Create AND switch in one step (modern way) git switch -c feature-login # Older command, still seen in tutorials git checkout -b feature-login Both git switch -c and git checkout -b do the same thing: create the branch and move you onto it. The switch command is newer and clearer, because checkout does several jobs. The official git branch command reference lists every option, but the small set here is what most developers use daily. Choose short, clear names like fix-header-bug. Your future self will thank you.
How to Switch Between Branches
Switching means moving to another signpost, so your files change to match that branch's latest commit. Git updates your folder for you, almost like changing the channel, except the channel is your own code at a different stage.
# Switch to an existing branch git switch main # Older way, same result git checkout main # Jump back to previous branch git switch - One warning that saves real pain: commit or stash unfinished work before you switch. If unsaved changes clash with the other branch, Git will either carry them along or refuse to switch. A quick git status first is a healthy habit, like checking your pockets before leaving the house.
The official Git website, git-scm.com, home of the free Git book and command reference.
How to Merge a Branch
Merging means combining work from one branch into another, usually bringing a finished feature into main. The routine is always the same: switch to the receiving branch, then run git merge with the other branch's name.
# Move to the receiving branch git switch main # Bring the feature into main git merge feature-login Git merges in two common ways. A fast-forward merge happens when main has not moved since you branched off. Git simply slides the main signpost forward to your newest commit, and history stays one straight line. A merge commit happens when both branches have new commits. Git then creates a special commit with two parents that ties the lines together. The free Pro Git book explains both with diagrams in its chapter on basic branching and merging.
Sometimes Git stops and reports a conflict. Nothing is broken. Both branches changed the same lines, and Git asks a human to choose. Edit the marked sections into the correct result, then run git add and git commit to finish.
Branch Commands at a Glance
| Command | What it does |
|---|---|
git branch | Lists all local branches |
git branch name | Creates a branch but stays where you are |
git switch -c name | Creates a branch and switches to it |
git switch name | Switches to an existing branch |
git merge name | Merges that branch into your current branch |
git branch -d name | Deletes a branch that has been merged |
git branch -D name | Force deletes a branch, merged or not |
git push -u origin name | Publishes a branch and links it to the remote |
How to Delete a Branch
Once a feature is merged, its branch has done its job. Keeping dozens of finished branches is like keeping empty pizza boxes: harmless for a while, then just clutter. Delete merged branches regularly.
# Safe delete: refuses if not merged git branch -d feature-login # Force delete: even if not merged git branch -D feature-login The small -d is the safe option. If the branch holds unmerged commits, Git stops and warns you, which has saved many developers. The capital -D deletes without asking, and commits that exist only there can be hard to recover. Use it only for a failed experiment you truly want gone. You cannot delete the branch you are standing on, so switch to main first.
Remote Branches and Push -u
So far everything happened on your own computer. A remote branch also lives on a hosting service like GitHub, so teammates can see it and you have a backup. If you mix up the tool and the website, our explainer on Git vs GitHub and what the difference really is clears that up in minutes.
# First push: publish and set upstream git push -u origin feature-login # After that, plain commands work git push git pull # Delete the remote branch too git push origin --delete feature-login The -u flag is short for upstream. It links your local branch to its remote partner, so plain git push and git pull then know where to go. You need it once per branch, on the first push. For remote branches, run git branch -r.
Common Mistakes Beginners Make
Working directly on main. It feels quicker today and painful tomorrow. Even for a tiny fix, a branch keeps main safe and releasable.
Forgetting which branch you are on. Run git status before you commit. Many "lost" commits are simply sitting on the wrong branch, feeling lonely.
Letting branches live forever. A branch that drifts for weeks falls behind main, and the final merge turns into conflict city. Merge little and often.
Using -D as the default. Force delete is for experiments you meant to throw away. If you reach for it daily, something needs attention.
Vague branch names. Names like test or branch2 tell you nothing a week later. Write the purpose in the name, for example fix-payment-rounding.
Frequently Asked Questions
What is the difference between git branch and git switch -c?
git branch name only creates the branch and leaves you where you are. git switch -c name creates it and moves you onto it in one step, so your next commits land in the right place.
Can I recover a deleted Git branch?
Often, yes, for a while. If it was merged, its commits are safe inside main. If you force deleted an unmerged branch, git reflog can sometimes find the last commit so you can recreate it, but there is no guarantee, so treat -D with respect.
How many branches can a Git project have?
There is no practical limit. Branches are tiny pointers, not folder copies, so hundreds cost almost nothing. The real limit is human: delete finished branches so the list stays easy to scan.
Should every task get its own branch?
Yes, that is the normal professional habit. One branch per feature, fix or experiment keeps changes separate, makes reviews cleaner, and lets a failed idea be deleted without touching stable code.
Conclusion
Branches are the feature that makes Git feel safe. Create one with git switch -c, work and commit without fear, merge it into main when ready, and delete it with the safe -d. Publish with git push -u origin the first time, and keep names clear and branches short lived. Practise the full cycle twice on a test project today, and branching will soon feel as natural as saving a file.


