How to View, Create, and Switch Git Branches: A Complete Guide

Updated: January 27, 2024 By: Guest Contributor Post a comment

Introduction

Branching is one of the most powerful features in Git. It allows developers to work on code independently from the main codebase. In this tutorial, we’ll explore how to view, create, and switch branches in Git. With hands-on examples and detailed explanations, you’ll soon master the ins and outs of branching in Git.

Understanding Branches in Git

In Git, branches are used to isolate development work without affecting other branches in the repository. Consider branches as a way to request a personal copy of the project. These copies can evolve in different directions but can be merged back into a mainline branch when the work is completed.

Viewing Branches

To view all of the branches in a Git repository, simply use the git branch command:

$ git branch

Output (may vary):

* master
  feature-x
  bugfix-y

This will list all the branches locally. The branch with an asterisk is your current branch.

Creating a New Branch

Creating a new branch in Git is extremely simple. Use the following command:

$ git branch 

To create a new branch named ‘feature-x’, enter:

$ git branch feature-x

Switching Branches

To switch to an existing branch, use the git checkout command:

$ git checkout feature-x

You can also create a new branch and switch to it directly using:

$ git checkout -b feature-x

Advanced Branching Operations

Now that we’ve covered the basics, let’s dive into some more advanced branching techniques.

Viewing All Branches

To see both local and remote branches, you can use:

$ git branch -a

Output (vary):

* master
  feature-x
  bugfix-y
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature-z

Renaming Branches

If you need to rename a branch, you can do so with the following commands:

To rename the current branch:

$ git branch -m new-branch-name

To rename a different branch:

$ git branch -m old-branch-name new-branch-name

Deleting Branches

If a branch is no longer needed, it can be deleted:

To delete a branch locally:

$ git branch -d branch-to-delete

To delete a branch remotely:

$ git push origin --delete remote-branch-to-delete

Merging Branches

Once your feature work is completed, you will want to merge your changes into the main branch. This can be done using:

$ git checkout master
$ git merge feature-x

Working with Branches in Collaborative Environments

In collaborative environments, dealing with branches gets more complex. Here are a few commands that you’ll find helpful.

Pushing Branches

When you’re ready to share a branch, you’ll need to push it to the remote repository:

$ git push origin feature-x

Pulling Updates

If someone else has updated a branch, you’ll need to pull the changes with:

$ git pull origin feature-x

Stashing Changes

When you need to switch branches but aren’t ready to commit, you can stash your changes:

$ git stash
$ git checkout another-branch
$ git stash pop

Conclusion

Mastering Git branches can enhance your development workflow significantly. By practicing creating, viewing, and switching between branches, you’ll find managing separate lines of development much easier. As you continue using Git, these commands will become second nature, empowering you to work more dynamically and collaboratively.