Creating a branch in Git | HTML KICK

Creating a branch in Git

Creating a branch in Git: Branch refers to a version of repository which diverges from the major working projects. It’s a feature accessible in a more modern version of the control systems. Git makes managing and creating branches simpler. The power & flexibility of the branching model is among the biggest benefit of Git. Git projects can consist of more than a single branch. Branches are a pointer for a snapshot of the changes. When a new feature needs to be added or fixed a bug, spawns some new branch to summarize the changes. Therefore, it’s difficult to merge unstable code with a major codebase & facilitates one to clean up upcoming history.

How to Creating a branch in Git?

There are a few methods in which one can create new branches at Git, with more of them different in how the branch is made from a major branch, whether this can be from the current branch, a tag, a different branch, etc.

The major common way of creating a new branch is

common way of creating a new branch is

That’s the most widely used syntax because it creates a branch for you from the current branch & it switches you to the branch in a single command.

Moreover one can also specify various branches from which new one will be made:

various branches from which new one will be made

Another different way is through using branch command directly.

Another different way is through using branch command directly

As seen from the example below, this does not automatically switch over to a new branch & keeps us on the current one:

automatically switch over to a new branch

When you need to work on a branch directly then you are required to switch to it yourself using the checkout command:

to switch to it yourself using the checkout command

Creating Branch from Commit

There are other few ways in which one can make new branches. One of these ways is through specifying a precise commit through its hash:

commit through its hash

As always when using Git, the whole hash does not require to be precise, just a few characters.

whole hash does not require to be precise

One can also utilize syntax, which will git create branch & check it out, wholly in a single command.

 git create branch & check it out

Creating Branch from Tag

This method is particularly useful as tags can be an excellent way for referencing a certain point in some project’s history.

certain point in some project's history

syntax can also be used

syntax can also be used

Creating a new branch from a remote branch

Here you need to take a remote branch as a basis for a new local branch, you can utilize the “–track” option:

remote branch as a basis for a new local branch

Moreover, one can also utilize the “checkout” command. When you need to name a local branch alike the remote one, all you need to have is to specify the name of the remote branch:

 specify the name of the remote branch

Creating a new branch in a remote repository

After operating on a new local branch, you may need to publish it in a remote repository, for sharing it with the team:

 publish it in a remote repository

“-u” flag informs Git to institute a “tracking connection”, which will make pulling & pushing easier in the future.

Git Master Branch

Master branch refers to a default branch at Git. It’s instantiated when the first commit is created on the project. When one makes the initial commit, you are given a master branch to begin the commit point. When one begins making some commit, the master branch pointer auto moves forward. Some repository just has one master branch.

Here at the master branch all the changes finally get merged back. This can be known as an official working version of the project.

Operations on Branches

On Git branches various operations can be performed. The git-branch command enables one to create, list, delete or rename branches. Other operations at branches are used by git checkout & git merge-command.


Other Important Topics

 

Terry White

Terry White is a professional Drupal & WordPress developer, Web developer, Web designer, Software Engineer, and Blogger. He strives for pixel-perfect design, clean robust code, and a user-friendly interface. If you have a project in mind and like his work, feel free to contact him

View all posts by Terry White →

Leave a Reply

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