The git branch allows you to create a new branch which means define a new direction in the development line. Here we are going to expose all cli commands to cover how to create a branch using git.
Table Of Contents
Anyway, the default branch name of git remote system is master which allow you adding changes and commits by default.
Let’s see how the git branch command works in the git bash.
Create a New Branch by using Git CLI
The following shows you the default of how to create a new branch using git bash command.
git branch [NEW_BRANCH_NAME]
This command will create a new branch but it doesn’t move the cursor into this new branch until you select it using another command.
However, you can create and change the current branch to another name using the following command.
git branch -m [NEW_BRANCH_NAME]
Or use switch word to move the cursor to another branch.
The following figure shows you, how the new branch was added to the tree.
Create a New Branch at a Specific Commit by using Git CLI
To do that you have to know what is the refs number of the specific commit. Let’s expose the history of refs numbers for the current project.
This will display you all commits with refs like the below image.
Anyway, I will create a new branch over the commit number 70c5b73. So I will specify this point beside the branch creation command.
git branch data 70c5b73
The following image shows you where is the beginning point of the new branch on the tree.
Let’s see how to create a new branch from the remote branch.
Create a New Branch from a Remote Branch
As you know, the git remote system has a master as a default branch but sometimes your colleagues are creating another branches on the project and the question, how do you will pull those remote branches into your local repository?
Actually, there are many ways to do that, you have to follow the below steps.
So firstly, you have to ask your colleague about his branch name to give you an access for it. then you have to create a new branch over your repository branch. And that can be done by using the following commands.
git branch [THE-REMOTE-BRANCH-NAME-HERE]
Then you have to select this branch by using the below command.
git switch [THE-REMOTE-BRANCH-NAME-HERE]
And then you need to pull the changes of this remote branch by the following command.
git pull origin [THE-REMOTE-BRANCH-NAME-HERE]
Anyway, these commands will allow you to retrieve the new branch files and changes from the remote server into your local repository.
Create a New Branch on the Remote Repository
It is so easy to do that, you have to use the previous command to create a new branch on your local repository, and then add the files and changes into this new branch then you have to push them by using the following command.
git push origin [YOUR-NEW-BRANCH-NAME]
Git Create Branch from Tag
Just use the following command to create a new branch from tag.
git branch [YOUR-NEW-BRANCH-NAME-HERE] [TAG-NAME-HERE]
In this article, you understood how to create a new branch on you local repository and how to select the new create branch to add the changes and files to it.