Sling Academy
Home/DevOps/How to View, Create, and Switch Git Branches: A Complete Guide

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

Last updated: January 27, 2024

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.

Next Article: What is HEAD in Git? Explained with Examples

Previous Article: Git: How to see commit history (git log)

Series: Git & GitHub Tutorials

DevOps

You May Also Like

  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide
  • Terraform: 3 Ways to Remove Duplicates from a List
  • Terraform: How to convert a number to a string and vice versa
  • Using bcrypt() and md5() functions in Terraform