Linux

Create and List Local and Remote Git Branches

Branches are part of the software development process and one of the most powerful features in Git. Branches are essentially pointers to a certain commit. When fixing a bug or working on a new feature, developers are creating a new branch that later can be merged into the main codebase. This article

Branches are part of the software development process and one of the most powerful features in Git. Branches are essentially pointers to a certain commit.

When fixing a bug or working on a new feature, developers are creating a new branch that later can be merged into the main codebase.

This article explains how to create and list local and remote Git branches.

List Git Branches

To list all local Git branches use the git branch or git branch --list command:

bash
git branch
bash
Output

   dev
   feature-a
   feature-b
   hotfix 
 * master

The current branch is highlighted with an asterisk *. In this example, that is the master branch.

In Git, local and remote branches are separate objects. If you want to list both local and remote branches pass the -a option:

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

The -r option will lists only the remote branches.

bash
git branch -r

Create a Git Branch

Creating a new branch is nothing more than creating a pointer to a given commit.

To create a new local branch, use the git branch command followed by the name of the new branch. For example, to create a new branch named cool-feature, you would type:

bash
git branch cool-feature

The command will return no output. If the branch with the same name already exists, you will see the following error message:

bash
Output

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

To start working on the branch and adding commits to it, you need to select the branch using git checkout:

bash
git checkout cool-feature

The command will output a message informing you that the branch is switched:

bash
Output

 Switched to branch 'cool-feature'

Instead of creating the branch and then switching to it, you can do that in a single command. When used with the -b option the git checkout command will create the given branch.

bash
git checkout -b cool-feature
bash
Output

 Switched to branch 'cool-feature'

From here, you can use the standard git add and git commit commands to new commits to the new branch.

To push the new branch on the remote repository, use the git push command followed by the remote repo name and branch name:

bash
git push remote-repo cool-feature

Conclusion

We have shown you how to list and create local and remote Git branches. Branches are a reference to a snapshot of your changes and have a short life cycle.

With the git branch command, you can also Rename and Delete local and remote Git branches.

Sponsored by WebSoft IT Development Solutions (Private) Limited

Nick Anonymous

Writes about Linux for Learn Cybers.

Related reading

Newsletter

Get smarter about security

Practical guides, tooling notes and the developments actually worth your attention — delivered when there is something worth saying.

No spam. Unsubscribe in one click.