📘Git and Github
Estimated reading: 3 minutes 350 views

Git Branch Explained – Create, Switch, Merge, and Delete Branches Easily


What is git branch?

In Git, a branch is like a pointer to a specific version of your project. It allows you to:

  • Work on new features safely
  • Experiment with ideas without affecting live code
  • Collaborate with teammates more efficiently
  • Keep the main branch (e.g., main) clean and stable

By default, every Git project starts with a branch called main (or sometimes master).


Why Use Git Branches?

Work on multiple things at once
Example: Alice works on a login feature; Bob works on search — on separate branches.

Test ideas safely
Example: You try a new design in a test branch without affecting your main site.

Team collaboration made easy
Example: Each developer pushes code to their own branch, then merges it into main.

Keep your project clean
Example: Only tested code is merged to main.


Benefits of Using Git Branches

Feature Description
Clean workflowFeature → test → merge into main
Parallel developmentDevelopers work on different features
Safe collaborationAvoids accidental overwrites
Better version trackingEasy to trace feature history

Common git branch Commands with Examples


1. List All Branches

git branch

Example Output:

* main
  feature-login
  bugfix-search

The branch with * is the one you’re currently on.


2. Create a New Branch

git branch contact-form

Creates a new branch called contact-form
Does not switch to it yet.


3. Switch to Another Branch

git checkout contact-form

You’re now working in the contact-form branch.


Shortcut: Create and Switch Immediately

git checkout -b about-page

This creates the about-page branch and switches to it in one step.


4. Delete a Branch (Locally)

git branch -d old-feature

Deletes a branch only if it’s already merged.

git branch -D unfinished-experiment

Force-deletes a branch even if not merged.


Real-World Branch Workflow Example

git branch feature-login          # Create branch
git checkout feature-login        # Switch to it

touch login.html
echo "<h1>Login Page</h1>" > login.html

git add login.html
git commit -m "Add login page"    # Save changes

git checkout main                 # Switch back to main
git merge feature-login           # Merge new feature
git branch -d feature-login       # Delete merged branch

Summary – Git Branch

Key Takeaways:

  • git branch helps you manage different features or tasks safely
  • Always use branches to isolate work before merging into main
  • Use checkout -b to create and switch in one command
  • Delete branches you no longer need to keep things clean

Real-World Relevance:

  • Prevents bugs from entering production
  • Encourages collaboration and organized version control
  • Works well with platforms like GitHub, GitLab, Bitbucket

FAQ Frequently Asked Questions – Git Branch


What’s the difference between git branch and git checkout?

  • git branch: Create or list branches
  • git checkout: Switch between them

How do I know which branch I’m on?

git branch

The branch with * is your current branch.


Can I create and switch to a branch at the same time?

git checkout -b new-feature

Yes! This creates and switches to new-feature.


Will switching branches delete my uncommitted work?

No, but Git will warn you if switching causes conflicts.

To temporarily save uncommitted changes:

git stash

What happens when I delete a branch?

It deletes the pointer to the branch on your local machine.

The commits still exist in Git history unless fully orphaned.


How do I push a new branch to GitHub?

After creating and committing:

git push origin new-branch-name

Example:

git push origin feature-login

Share Now :
Share

Git Branch

Or Copy Link

CONTENTS
Scroll to Top