LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
Git & GitHub

Git Branching – Create, Switch and Manage Branches

A branch lets you work on a feature or fix without directly changing the main development line. You can develop independently, test your changes, and merge them when they are ready.

Git Branching – Create, Switch and Manage Branches

Imagine you are working on an employee management application. The existing application is running correctly, but your team needs to add a new employee approval workflow. You want to develop this feature without disturbing the existing code.

Would you directly modify the main codebase? In a team environment, that can create unnecessary risks. A better approach is to create a separate branch, implement the feature there, test it, and later integrate it into the main development line.

This is exactly what Git branching helps developers achieve.

In this article, you will learn:
  • What a Git branch is and how it works internally.
  • How to create, list, switch, rename and delete branches.
  • The difference between git switch and git checkout.
  • How to publish a local branch to GitHub.
  • How teams use branches to develop features independently.

1. What Is Git Branching?

A Git branch is a movable reference to a commit. It gives you an independent line of development within the same repository.

When you create a branch, Git does not copy your entire project into another folder. It creates a new reference that initially points to the same commit as the branch from which it was created.

As you create new commits on that branch, its reference moves forward. Other branches can continue pointing to their own commits.

Why do developers use branches?

  • Feature development: Build a new feature without changing the main development line.
  • Bug fixing: Isolate a fix so it can be reviewed and tested independently.
  • Parallel development: Allow multiple developers to work on different tasks.
  • Experimentation: Try an implementation without immediately affecting stable code.
  • Code review: Submit changes through a pull request before integration.

2. Understanding How Git Branches Work

Git stores project history as commits. A branch is a lightweight name pointing to one of those commits. The special reference called HEAD tells Git which branch or commit is currently checked out.

Consider a repository with a main branch. You create a feature branch from it. Both branches initially point to the same commit. When you commit a change on the feature branch, only that branch moves forward.

The main branch continues horizontally while a feature branch starts from an earlier commit and develops independently. main feature/approval A B C D Feature 1 Feature 2 Branch point Each branch can advance independently

The feature branch starts from commit B. New feature commits do not automatically move the main branch.

Important Git concepts

Core branching terminology
Concept Meaning
Branch A movable reference to a commit.
HEAD Identifies the currently checked-out branch or commit.
Commit A saved snapshot in the repository history.
Working tree The files currently checked out in your project directory.
Index / staging area The proposed changes prepared for the next commit.
Remote-tracking branch A local reference representing a branch observed on a remote repository.

3. Check Your Current Branch

Before creating or switching branches, check which branch is currently active.

Command 1: List local branches

git branch

Example output

* main
  feature/employee-search
  bugfix/login-validation

The asterisk (*) identifies the branch currently checked out. In this example, the active branch is main.

Command 2: Show branch and working tree details

git status

Example output

On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

4. Create a New Branch

Suppose your team is adding an employee approval workflow. You can create a branch named feature/employee-approval.

Command 1: Create a branch

git branch feature/employee-approval

This creates the branch, but it does not switch your working directory to that branch. Your current branch remains active.

Command 2: Verify the new branch

git branch

Example output

  feature/employee-approval
* main
Remember: git branch branch-name creates a branch only. It does not automatically switch to it.

5. Switch Between Branches

After creating a branch, you need to switch to it before making changes on that branch.

Using git switch

git switch is the modern, dedicated command for switching branches.

Command

git switch feature/employee-approval

Example output

Switched to branch 'feature/employee-approval'

Create and switch in one command

Instead of running two separate commands, you can create a branch and switch to it immediately.

Command

git switch -c feature/employee-approval

Example output

Switched to a new branch 'feature/employee-approval'

Using the older git checkout command

Older Git workflows commonly use git checkout for branch operations.

Switch to an existing branch

git checkout feature/employee-approval

Create and switch to a new branch

git checkout -b feature/employee-approval

Both approaches are valid. For modern Git workflows, prefer git switch when the intention is specifically to change branches.

6. Understand git switch vs git checkout

The git checkout command has multiple responsibilities. It can switch branches and also check out files or commits. Git introduced git switch and git restore to make these intentions clearer.

Comparison of branch-related commands
Command Purpose Recommended usage
git switch branch Switch to an existing branch. Preferred for branch switching.
git switch -c branch Create and switch to a branch. Preferred for new branches.
git checkout branch Switch to an existing branch. Valid legacy command.
git checkout -b branch Create and switch to a branch. Valid legacy command.
git restore file Restore file content in the working tree. Use for file restoration.

7. Rename a Branch

Branch names should communicate the purpose of the work. If a branch name is unclear or incorrect, you can rename it.

Rename the current branch

git branch -m feature/employee-approval

Rename another local branch

git branch -m old-feature-name new-feature-name

The -m option renames a local branch. If the old branch has already been pushed to GitHub, renaming it locally does not automatically rename the remote branch.

8. Delete a Branch

Once a feature has been integrated and is no longer required, you can remove its local branch.

Safely delete a merged branch

git branch -d feature/employee-approval

Git checks whether the branch has been fully merged into its upstream branch or the current branch. If it has not been merged, Git normally refuses the deletion.

Force delete a local branch

git branch -D feature/employee-approval
Be careful: git branch -D force-deletes the local branch even when Git considers it unmerged. Make sure you do not need its commits before using this command.

Delete a remote branch

To delete a branch from the remote repository on GitHub:

git push origin --delete feature/employee-approval

This removes the remote branch. It does not automatically delete your local branch.

9. Work with Remote Branches

In a team environment, branches are often shared through a remote repository such as GitHub. A local branch exists on your computer, while a remote branch exists in the remote repository.

List local branches

git branch

List remote-tracking branches

git branch -r

List all local and remote-tracking branches

git branch -a

Publish a new branch to GitHub

After creating a local branch and committing your work, publish it using:

git push -u origin feature/employee-approval

The -u option sets the upstream relationship. For subsequent pushes and pulls, Git can use that configured upstream branch by default.

Example output

Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), done.
 * [new branch]      feature/employee-approval -> feature/employee-approval
branch 'feature/employee-approval' set up to track 'origin/feature/employee-approval'.

Update remote-tracking information

git fetch origin

This downloads new objects and updates remote-tracking references. It does not automatically merge those changes into your current local branch.

10. Practical Example: Develop a Feature Using Branches

Let's follow a simple employee management workflow. The main application is stable, and a developer needs to add employee search functionality.

Assume that:

  • The repository already exists.
  • The local main branch is available.
  • The working tree is clean.
  • The remote repository is configured as origin.

Step 1: Start from the main branch

git switch main
git pull origin main

This switches to the local main branch and integrates updates from origin/main.

Step 2: Create a feature branch

git switch -c feature/employee-search

Step 3: Implement the feature

Update the employee search API, add the required validation, and write tests. Your changes now belong to the feature branch's working tree.

Step 4: Review and stage your changes

git status
git add .
git status

Review the files before staging. In production work, prefer staging specific files when you do not intend to include every changed file.

Step 5: Commit the feature

git commit -m "Add employee search feature"

Step 6: Publish the branch

git push -u origin feature/employee-search

Step 7: Open a pull request

Open GitHub, navigate to the repository, and create a pull request from feature/employee-search into main. Add a useful description, request a review, and ensure required checks pass.

Step 8: Clean up after integration

After the pull request is merged, update your local main branch and remove the feature branch if it is no longer needed.

git switch main
git pull origin main
git branch -d feature/employee-search
Important: A branch is not a separate repository. All branches in a repository share the same underlying object database and commit history.

11. Common Branching Mistakes

Common mistakes and how to avoid them
Mistake Why it is a problem Better approach
Working directly on main Unreviewed changes can affect the shared development line. Use a feature or bug-fix branch.
Creating a branch but not switching Your changes may continue going to the previous branch. Use git switch branch-name.
Using unclear names Team members cannot easily identify the branch purpose. Use descriptive names such as feature/employee-search.
Force-deleting branches casually Unmerged commits may become difficult to recover. Prefer git branch -d.
Not publishing the branch Other team members cannot access your local-only work. Push it to the remote repository.
Creating a feature from an outdated base The branch may miss recent changes from main. Update the base branch before starting.

12. Branch Naming Best Practices

A consistent naming convention makes a repository easier to understand and manage.

Examples of useful branch names
Branch type Example
Feature feature/employee-search
Bug fix bugfix/login-validation
Hotfix hotfix/payment-timeout
Release preparation release/2.4.0
Experiment experiment/search-index
  • Use lowercase letters and hyphens for readability.
  • Keep branch names short but meaningful.
  • Include a ticket or issue identifier when your team requires it.
  • Avoid spaces and vague names such as test or new-branch.
  • Delete completed branches according to your team's repository policy.

13. Git Branching in a Real Team Workflow

Most development teams use branches to isolate work and control how changes enter the shared codebase.

A developer creates a feature branch, commits and pushes changes, opens a pull request, receives review, and merges into main. Create featurebranch Implement andcommit changes Push branchto GitHub Open pullrequest Review andrun checks Merge intomain Changes reach main only after the team's integration process

A typical feature branch workflow. Teams can configure additional approvals, automated checks and protection rules before merging.

Branching is only one part of collaboration. The next step in this learning series is understanding how Git combines work from different branches using merge operations.

14. Useful Git Branch Commands – Quick Reference

Git branching command cheat sheet
Command Purpose
git branch List local branches.
git branch name Create a branch.
git switch name Switch to a branch.
git switch -c name Create and switch to a branch.
git branch -m new-name Rename the current branch.
git branch -d name Safely delete a merged local branch.
git branch -D name Force-delete a local branch.
git branch -r List remote-tracking branches.
git branch -a List local and remote-tracking branches.
git push -u origin name Publish a branch and set its upstream.
git push origin --delete name Delete a remote branch.
git fetch origin Update remote-tracking information.

15. Interview Questions and Answers

1. What is a Git branch?

A Git branch is a movable reference to a commit. It allows developers to maintain an independent line of development without immediately changing another branch.

2. What is the difference between git branch and git switch?

git branch is used to list, create, rename and delete branches. git switch changes the currently checked-out branch and can also create a branch using -c.

3. What is the difference between git switch -c and git checkout -b?

Both commands create a new branch and switch to it. git switch -c is the newer, focused command, while git checkout -b is the older combined checkout command.

4. What is the difference between git branch -d and git branch -D?

git branch -d performs a safety check and normally deletes only a branch Git considers merged. git branch -D forces deletion even if the branch has unmerged commits.

5. What is a remote-tracking branch?

A remote-tracking branch is a local reference that records the state of a branch on a remote repository, such as origin/main. It is updated by commands such as git fetch.

6. Does creating a branch copy the entire repository?

No. A branch is a lightweight reference to a commit. Git does not create a complete copy of the repository for every branch.

7. What happens if you switch branches with uncommitted changes?

Git may allow the switch if the changes can be carried safely without being overwritten. If switching would overwrite conflicting working-tree changes, Git refuses. Commit, stash or otherwise safely handle your changes before switching.

16. Hands-On Practice

Try these exercises in a local Git repository. Use a practice repository rather than experimenting on important production work.

Exercise 1: Create a branch

Create a branch named feature/customer-registration.

Expected result: The branch should appear when you run git branch.

Exercise 2: Switch branches

Switch to the new branch using git switch.

Expected result: The asterisk in git branch should appear next to the new branch.

Exercise 3: Create and switch in one command

Create bugfix/customer-validation using a single command.

Expected result: Git should create the branch and switch to it.

Exercise 4: Rename a branch

Rename bugfix/customer-validation to bugfix/registration-validation.

Expected result: The updated name should appear in your local branch list.

Exercise 5: Publish a branch

Push your feature branch to GitHub and configure its upstream branch.

Expected result: The branch should be visible in the remote repository.

17. Conclusion

Git branching is one of the most important concepts in professional software development. It allows developers to work on features, bug fixes and experiments independently while keeping the main development line organised.

Remember these essential commands:

  • git branch – list or create branches.
  • git switch – move between branches.
  • git switch -c – create and switch in one step.
  • git branch -d – safely delete a merged branch.
  • git push -u – publish a branch and set its upstream.

Once you are comfortable creating and managing branches, the next concept to learn is how to combine changes from different branches using Git merge.

18. Official Documentation and Further Learning

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.