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 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:

 git branch 
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:

 git branch -a 
   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.

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:

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:

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:

 git checkout cool-feature 

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

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.

 git checkout -b cool-feature 
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:

 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

Recent Posts

How To Set Up Secure Nginx Server Blocks on Ubuntu 22.04

NGINX Server Nginx, a popular open-source web server, excels at handling high traffic websites efficiently.… Read More

6 days ago

The Web Server Showdown: Nginx vs. Apache, LiteSpeed, Caddy, and Beyond

In the realm of web hosting, choosing the right web server is paramount. It acts… Read More

6 days ago

Linear guidance systems

Are indispensable for ensuring smooth, precise linear motion in many industrial applications. Whether in robotics,… Read More

2 months ago

Cyber Attack Statistics – Identifying Vulnerabilities and Strengthening Defenses

Cyber attacks are becoming more frequent, complex, and damaging. They can disrupt critical operations and… Read More

3 months ago

Empowering Cybersecurity in 2024 with XDR for Comprehensive Threat Detection and Response

With the rise of new threats and the increasing complexity of IT environments, organizations need… Read More

3 months ago

Facade Design Pattern: Simplifying Complex Systems

1. Introduction In software design, managing complex systems can be challenging. The Facade Design Pattern… Read More

5 months ago