Visit Sponsor

Written by 1:22 pm Blog

Create & Manage New Branch in Git – Step‑by‑Step Guide

Branching is one of Git’s most powerful features. It enables developers to isolate work, manage parallel development, and collaborate effectively without interfering with the main codebase. Whether you’re implementing a feature, fixing a bug, or experimenting, understanding Git branching is essential for modern development workflows.

This updated guide on javatechig.com explains how to create, switch, merge, delete, and manage branches in Git with practical examples and best practices used in real‑world teams.

What Is a Git Branch?

A branch in Git is a pointer to a snapshot of your project history. By default, all repositories start with a main (or master) branch. When you create a new branch, Git makes a lightweight pointer that lets you develop in isolation without affecting the main code.

Branches are crucial for:

  • Feature development
  • Bug fixes
  • Code reviews
  • Parallel efforts in teams

Creating a New Branch

Show Existing Branches

git branch

This displays all local branches.

Create a New Branch

git branch feature/login

This creates a branch named feature/login but does not switch to it.

Switching Between Branches

To move to another branch:

git checkout feature/login

Or combine creation and switch:

git switch -c feature/login

The switch -c command is a modern alternative to checkout and is preferred for clarity.

Verify Current Branch

After switching:

git status

The output shows which branch you are currently on.

Making Changes on a Branch

Once on your branch, make your code changes, stage, and commit:

git add .
git commit -m "Implement login UI"

Each branch keeps its own commit history until merged.

Pushing Branch to Remote

Initial Push

git push -u origin feature/login

The -u flag sets the upstream branch so future pushes can be done with a simple git push.

Merging a Branch

After completing work, merge your feature branch into main:

Checkout Target Branch

git switch main

Merge

git merge feature/login

If there are no conflicts, Git fast‑forwards or creates a merge commit.

Handling Merge Conflicts

Identify Conflicts

During merge, Git may show conflict markers:

<<<<<<< HEAD
Code from main
=======
Code from feature/login
>>>>>>> feature/login

Manually resolve the conflicts, then:

git add .
git commit -m "Resolve merge conflicts"

Deleting a Branch

Delete Local Branch

git branch -d feature/login

If the branch hasn’t been merged yet and you’re sure:

git branch -D feature/login

Delete Remote Branch

git push origin --delete feature/login

This removes the branch from the remote repository.

Branch Naming Best Practices

Use descriptive, consistent names:

  • feature/<description> – new features
  • bugfix/<description> – bug fixes
  • hotfix/<description> – urgent fixes
  • release/<version> – preparing releases

Use hyphens and lowercase to improve readability and tooling support.

Working with Remote Branches

Fetch Updates

git fetch

This updates your local view of remote branches.

List Remote Branches

git branch -r

Track a Remote Branch

git switch --track origin/feature/login

This sets up local tracking for remote branches.

Rebase Instead of Merge (Advanced)

Rebase rewrites commits for a linear history:

git switch feature/login
git rebase main

This reapplies your changes on top of updated main. Use with caution — avoid rebasing public/shared branches.

Best Practices (2026 Updated)

  • Always work on feature branches
  • Prefer git switch over checkout for clarity
  • Set upstream tracking on remote pushes
  • Resolve conflicts early and communicate with team
  • Use pull requests for collaboration and code review

Practical Workflow Summary

  1. Create and switch to branch: git switch -c feature/example
  2. Commit changes: git commit -am "Implement example"
  3. Push to remote: git push -u origin feature/example
  4. Merge to main via PR or merge command
Visited 9 times, 2 visit(s) today
Close