To advertise with us contact on Whatsapp: +923041280395 For guest post email at: itsaareez1@gmail.com

Create and List Local and Remote Git Branches

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

Leave a Reply

Your email address will not be published. Required fields are marked *