Git Branching for Beginners: Create, Switch, Merge and Clean Up Branches
Learn Git branching step by step: what a branch is, the commands to create, switch, merge and delete branches, how to fix conflicts, and a simple workflow.
- Published
- Updated
- 4 min read

Key takeaways
- A Git branch is just a lightweight pointer to a commit, so creating one is instant.
- Use git switch -c to create a branch, git merge to combine work and git branch -d to clean up.
- Keep branches short-lived and merge through reviewed pull requests to avoid painful conflicts.
On this page
Branching is the feature that makes Git so useful for real projects. It lets you build a new feature, try an experiment or fix a bug without touching the working version of your code, and then bring those changes back in when they are ready. If branches have ever felt confusing, this guide walks through what they are and the handful of commands you actually need.
What a branch really is
In Git, a branch is simply a movable pointer to a commit. When you make a new commit on a branch, the pointer moves forward to it. A special pointer called HEAD tells Git which branch you currently have checked out.
Because a branch is just a small reference, creating one is instant and costs almost nothing. That is why Git workflows encourage creating a branch for every piece of work.
Before you start
Check which branch you are on and whether you have uncommitted changes:
git status
git branch
git branch lists local branches and marks the current one with an asterisk. Add -a to include remote branches.
Creating and switching branches
Create a new branch and switch to it in one step:
git switch -c feature/login-form
Switch back to an existing branch:
git switch main
The git switch command was added in Git 2.23. On older versions, or in older tutorials, you will see the equivalent git checkout -b feature/login-form and git checkout main.
Working on a branch
Work normally: edit files, then stage and commit.
git add src/login-form.js
git commit -m "Add login form validation"
These commits exist only on feature/login-form. Your main branch is untouched. To share the branch with teammates or back it up, push it to the remote:
git push -u origin feature/login-form
The -u flag sets the upstream, so future git push and git pull commands on this branch know where to go.
Merging a branch
When the feature is finished, switch to the branch you want to merge into and merge:
git switch main
git pull
git merge feature/login-form
Pulling first makes sure your local main is up to date. Git then does one of two things:
- Fast-forward merge: if
mainhas not changed since you branched off, Git just moves themainpointer forward. No extra commit is created. - Three-way merge: if both branches have new commits, Git creates a merge commit that combines them.
On team projects, merging usually happens through a pull request (or merge request) on GitHub, GitLab or Bitbucket, so changes can be reviewed and tested first.
A hands-on freeCodeCamp walkthrough of creating, switching and merging branches, useful if you prefer to follow along visually.
Resolving merge conflicts
A conflict happens when both branches changed the same lines of the same file. Git stops and marks the file:
<<<<<<< HEAD
const timeout = 30;
=======
const timeout = 60;
>>>>>>> feature/login-form
To resolve it:
- Open the file and decide what the final code should be. Keep one side, the other, or a combination.
- Delete the conflict markers (
<<<<<<<,=======,>>>>>>>). - Stage the file and complete the merge:
git add config.js
git commit
If you want to back out entirely, git merge --abort returns you to the state before the merge started. Most code editors also offer a visual conflict resolver, which is often easier than editing markers by hand.
Deleting branches after merging
Merged branches clutter the list. Delete the local branch:
git branch -d feature/login-form
The lowercase -d refuses to delete a branch with unmerged work, which protects you from mistakes. Use -D only when you are sure you want to discard it. Delete the remote branch with:
git push origin --delete feature/login-form
A simple workflow for small teams
A widely used, lightweight approach looks like this:
mainalways contains working, deployable code.- Every task gets its own short-lived branch from
main. - Commit small, focused changes with clear messages.
- Open a pull request early to get feedback.
- Keep your branch current by merging or rebasing from
mainregularly. - Merge after review and passing tests, then delete the branch.
Short-lived branches are the key. The longer a branch lives, the more it drifts from main and the harder the eventual merge.
Quick reference
| Task | Command |
|---|---|
| List branches | git branch |
| Create and switch | git switch -c name |
| Switch | git switch name |
| Push new branch | git push -u origin name |
| Merge into current branch | git merge name |
| Abort a merge | git merge --abort |
| Delete merged branch | git branch -d name |
| Delete remote branch | git push origin --delete name |
Conclusion
Once you see a branch as a lightweight pointer, Git's branching model becomes much less intimidating. Create a branch for each task, commit often, merge through reviewed pull requests and clean up afterwards. These few commands cover the large majority of day-to-day Git work. If you deploy your projects to the cloud, our explainer on IaaS, PaaS and SaaS is a good next read.
Frequently asked questions
What is the difference between git switch and git checkout?
git checkout does several unrelated jobs: switching branches, restoring files and more. Git 2.23 introduced git switch for changing branches and git restore for restoring files, so each command has one clear purpose. git checkout still works.
Should I use merge or rebase?
Both integrate changes. Merge preserves the exact history and is safe on shared branches. Rebase rewrites your commits onto a new base to create a linear history and should only be used on branches nobody else is working on. Many teams merge pull requests and let developers rebase their own local work.
Is it main or master?
Either name works. Git's default for new repositories has historically been master, but many hosting services, including GitHub, now default to main. You can set your preference with git config --global init.defaultBranch main.
Sources and further reading
Related articles
Jensen Huang Is Back as Nvidia CEO: Why the Board Reversed Its Decision and What Comes Next
Nvidia's board has reinstated Jensen Huang as CEO, 11 days after he resigned. What the board said, why it changed course and what still has to be settled.
Cisco Expressway CVE-2026-76466 Under Attack: How to Check Your Cluster and Patch to X15.5.20
CISA says attackers are exploiting a critical Cisco Expressway flaw that gives root access on clustered devices. Who is affected, how to check and how to patch.

Vulcan Rocket Failure Destroys 40 Amazon Leo Satellites: What Happened and What It Means for Europe
A ULA Vulcan rocket failed 10 minutes after launch and lost 40 Amazon Leo satellites. What happened, what it means for Amazon's Starlink rival and Ariane 6.