How to create a branch in Git

If you are working in a team or making complicated projects, then it's very likely that you'll need to create branches on an ongoing basis. As a result, you should know which commands have to be used for that purpose.

The GIT version control system contains all the functionality required for managing the branches. In this article, we will describe how to create a new local branch. There are various methods, so please review all the available options in the following sections.

Creating a local branch

To create a branch, you should use the "git branch" command. It's very simple, you just have to provide the branch name. Please see the specification below.

git branch <branch-name>

But please pay attention that this command will not switch you to the new branch. You have to manually run the "git checkout" command with the name of the new branch, then it will become active.

git checkout <branch-name>

However, there is an alternative method of creating a branch. You can just run a single command that will do both operations. It will create the branch first and then will execute the checkout operation to switch to the branch. Please see the specification below.

git checkout -b <branch-name>

Creating a branch from existing branch

You may also create a new local branch from the existing one. To do that, you have to run the "git branch" command and provide names of both new and existing branches. Please see how it can be used below:

git branch <new-branch-name> <existing-branch-name>

After that, you may also switch to the new branch. Example is shown below.

git checkout <new-branch-name>

The alternative method is using just a single command, which will switch to the new branch automatically once it is created. Specification is given below.

git checkout -b <new-branch-name> <existing-branch-name>

Creating a branch from existing commit

In the same way as you create a branch from another branch, you can also create a new branch from some commit. The specification is given below, you have to provide the name of the new branch and the ID of a commit.

git branch <new-branch-name> <commit>

Then, you can switch to the new branch with the command like the following:

git checkout <new-branch-name>

Specification of the single command method of creating a branch from the commit is shown below.

git checkout -b <new-branch-name> <commit>

Creating a branch from existing tag

And finally, you can also create a new branch from the tag. The command is very similar to the previous example, the only difference is that you have to specify the tag name as a last argument.

git branch <new-branch-name> <existing-tag-name>

To switch to the new branch, please use a command like the following:

git checkout <new-branch-name>

This task can also be done with a single command only, see it below.

git checkout -b <new-branch-name> <existing-tag-name>

Getting name of the current branch

After some time of working with the local copy of the repository, you may not be sure which branch you are actually using. Then the following command may help you.

git branch

It will print the name of the current branch. The output will be like the following:

[developer@localhost repository]$ git branch
* master

Pushing a branch to the repository

As you have noticed, the steps provided above are for creating a local branch only. However, once some changes are added, you may wish to save the files to the remote repository.

This is for you to decide when to push the work. For example, this can be done when some new feature is implemented and ready to be used by other developers.

Following are the instructions that will allow you to push the specific branch to the remote repository.

At the beginning, all the files have to be added to the index. This can be done for the whole repository with such a command:

git add .

Or you can add specific files only, see the example below:

git add <your-file-name>

After that, you need to create a new commit.

git commit -m "Your message here"

Now, it's time to push the changes to the remote repository. The "git push" command should be called with the name of the repository and also the name of the branch.

git push origin <new-branch-name>

Reviewing a list of all branches

To view a list of all the branches which are available, you may run the "git branch" command with the "-a" argument. See how it should be done below:

git branch -a

The output will vary depending on the repository, but you can see our example below.

[developer@localhost repository]$ git branch -a
* master
remotes/origin/1.0-branch
remotes/origin/2.0-branch
remotes/origin/3.0-branch

Deleting a branch

If some branch has been created by an accident or you do not need a specific branch, then it can be deleted. For that purpose, the "git branch" command should be called with the "-d" argument and name of the branch to delete. Please see the specification below.

git branch -d <branch-name>

However, the above command will not work if you have modified some files. If you are sure that those changes are not needed, you can call the "git branch" command with the "-D" argument. It will force the deletion of the files even if they have been modified.

See example of how this should be done below:

git branch -D <branch-name>

But please be careful with such an operation, as the data can be lost. You may wish to make a backup prior to deleting the branch.

In addition, you can delete a remote branch. For that, you may run the command that is described below.

git push origin --delete <branch-name>

Conclusion

As you see, the GIT version control system contains a rich set of tools that allow you to manage the branches. The same task can be done in different ways, you can use either the "git branch" or "git checkout" commands to create a new local branch.

Also, it's possible to create a branch that is based on some specific commit or tag. This can be useful if you are working on a complex project and need to improve something that has already been done in the past.

Thank you very much for reading this article. Do not hesitate to leave your comments or questions in the form below.

Leave a Reply

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