Oct 18, 2023 3 min read

How to Create and List Local and Remote Git Branches

Create and List Local and Remote Git Branches with our step-by-step tutorial. Git is a popular open-source distributed version control system.

Create and List Local and Remote Git Branches
Table of Contents

Introduction

Before we go into how to create and list local and remote Git branches, first let's understand - what is Git?

Git is a popular open-source distributed version control system that can handle a wide range of projects, from small to large, quickly and efficiently. It stands out since it is user-friendly and simple to understand.

In this tutorial, you will create and list local and remote Git branches. We will also address a few FAQs on how to create and list local and remote Git branches.

List Git Branches

Use the git branch or git branch --list commands to see a list of all local Git branches:

git branch
Output

dev
  feature-a
  feature-b
  hotfix
* master

With an asterisk *, the current branch is highlighted. That is the master branch in this case.

Local and remote branches are distinct objects in Git. Pass the -a option if you wish to list both local and remote branches:

git branch -a
Output

dev
  feature-a
  feature-b
  hotfix
* master
  remotes/origin/regression-test-a
  remotes/origin/regression-test-b

Git's -r option instructs it to only list remote branches.

git branch -r

Create a Branch

Generating a new branch is essentially the same as creating a pointer to a certain commit.

Use the git branch command followed by the name of the new branch to establish a new local branch. To create a new branch named cool-feature, for example, type:

git branch cool-feature

There will be no output from the command. If a branch with the same name already exists, the following error message will appear:

Output

fatal: A branch named 'cool-feature' already exists.

To begin working on the branch and making commits to it, use git checkout to choose the branch:

git checkout cool-feature

The command will print a message stating that the branch has been switched:

Output

Switched to branch 'cool-feature'

You can accomplish everything in one command instead of creating the branch and then switching to it.

The git checkout command, when used with the -b option, creates and switches into the specified branch:

git checkout -b cool-feature
Output

Switched to branch 'cool-feature'

You can now add commits to the new branch using the regular git add and git commit commands.

Use the git push command with the remote repo name and branch name to push the new branch to the remote repository:

git push remote-repo cool-feature

FAQs to Create and List Local and Remote Git Branches

How do I create a new local Git branch? 

To create a new local Git branch, you can use the command git branch <branch-name>. For example, to create a branch named "feature-branch", you would run git branch feature-branch.

How can I switch to a newly created branch? 

To switch to a newly created branch, use the command git checkout <branch-name>. For instance, git checkout feature-branch will switch to the "feature-branch" branch.

How do I create a new remote Git branch? 

To create a new remote Git branch, you first need to push a local branch to the remote repository using the command git push -u origin <branch-name>. This command creates the branch on the remote repository and establishes a tracking relationship.

How can I list all local Git branches? 

To list all local Git branches, run the command git branch. This will display a list of all branches with an asterisk (*) indicating the currently active branch.

How can I list all remote Git branches? 

To list all remote Git branches, use the command git branch -r. This command will show you a list of all branches in the remote repository.

How do I delete a local Git branch? 

To delete a local Git branch, you can use the command git branch -d <branch-name>. For example, git branch -d feature-branch would delete the "feature-branch" branch.

Can I rename a remote Git branch?

Yes, you can rename a remote Git branch by following a few steps. First, delete the old remote branch using git push origin --delete <old-branch-name>. Afterward, push the renamed local branch to the remote repository using git push origin <new-branch-name>. This effectively renames the branch on the remote repository.

Conclusion

We've shown you how to list and create Git branches locally and remotely. Branches are a one-time reference to a snapshot of your modifications with a short lifespan.

You can also Rename and Delete local and remote Git branches using the git branch command.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.