Skip to content
Programming & Coding

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
Developer workstation at night with a monitor showing a colorful Git commit graph of branching and merging lines
Developer workstation at night with a monitor showing a colorful Git commit graph of branching and merging lines. AI-generated illustration.

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
  1. What a branch really is
  2. Before you start
  3. Creating and switching branches
  4. Working on a branch
  5. Merging a branch
  6. Resolving merge conflicts
  7. Deleting branches after merging
  8. A simple workflow for small teams
  9. Quick reference
  10. Conclusion

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 main has not changed since you branched off, Git just moves the main pointer 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.

Git Branches Tutorial · freeCodeCamp.org
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:

  1. Open the file and decide what the final code should be. Keep one side, the other, or a combination.
  2. Delete the conflict markers (<<<<<<<, =======, >>>>>>>).
  3. 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:

  1. main always contains working, deployable code.
  2. Every task gets its own short-lived branch from main.
  3. Commit small, focused changes with clear messages.
  4. Open a pull request early to get feedback.
  5. Keep your branch current by merging or rebasing from main regularly.
  6. 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

TaskCommand
List branchesgit branch
Create and switchgit switch -c name
Switchgit switch name
Push new branchgit push -u origin name
Merge into current branchgit merge name
Abort a mergegit merge --abort
Delete merged branchgit branch -d name
Delete remote branchgit 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

  1. Pro Git book – Git Branching (git-scm.com)
  2. Git documentation – git-switch
  3. Git documentation – git-merge