How to Create Branch in Git

Photo of author

By admin

Branching in Git means to diverge from the main developing line and make changes in different branches. This is done to not disturb the development process in the mainline. Branching operations in Git are simpler and instantaneous. It can merge unlimited branches in a single day that aids the workforce. Git saves data as snapshots by storing commit objects.

Creating new branches gives the developer a new area to perform in away from a single branch. It can be used for various purposes. For multiple collaborators in a git repository, a unique branch can be created for each of them. Then the code can be committed to that specific branch to avoid overwriting.

Find the Number of Branches Present

On a standard terminal, simply typing ‘git status‘ can tell the user which branches they are currently on. To check remote branches, the command ‘git branch -r’ should be used. To check both local and remote branches, ‘git branch -a‘ has to be run.

To check all existing branches and new branches created, the user must type ‘git branch –list‘. This will list all the branches created and star mark the branch the user is currently on. For example, if the user is on the branch ‘master’ and has created a branch name ‘beta’, the list will show beta and *master.

Default branch- The default branch is ‘master’ for all developers.

How to Create Branches

After creating a remote repository on the github and connecting them to the local repository, new branches can be created. To create a new branch, the user needs to type ‘git branch branch-name’. The branch name should be normal to avoid getting any errors or any unnecessary confirmation messages.

Branches can also be made manually on the git repository. However, it is advised that users should manage such actions inside the terminal because of more control and lesser risk of mistakes. Also, the common line tool is faster than a graphic user interface.

Git checkout

A checkout is used in Git to change target entities. This command operates among files, commits, and branches. To switch to the new branch created, the user needs to type ‘git checkout branch-name’. Therefore, in the previous case, the command ‘git checkout beta‘ needs to be typed. To remove old commits and files from the working and updating commands, ‘git checkout’ has to be used. Git checkout command changes the target of the main reference, which makes it an essential Git tool.

Git clone- Often confused with git checkout, git clone fetches a code from the remote workplace but doesn’t have the functionality of switching between two codes.

Shortcut- To avoid the above lengthy process, a branch name can also be created in one go. The user needs to type the command, ‘git checkout -b branch name‘. The -b specifies that the user needs to create a branch. By using such shortcuts, users can switch directly to a new branch.

The Process of Using Branches

Git supports the branch concept that allows users to create various branches of code to develop codes on all of them. After the final product is complete, all these branches can be merged into one file. The default branch for any user is ‘master’. For example, a code the user is experimenting on is on ‘master’. Then, he has to add that code to the new branch created and then ‘commit’ the changes to the new branch.

Commit changes

To commit changes into the newly created branch, the command is ‘git commit -m new code’. However, to change codes in the master branch, users would need to check out from the new branch and then make necessary changes. This is because changes made in the new branch are not saved in the master branch.

Merging various branches

Merging means putting different independent branches into a single branch. Git can’t automatically merge separate branches when a merge commit is created. When data has been changed in both branches, it can’t combine them because of its version control conflict feature. In such cases, conflicts have to be resolved manually. A few steps are needed before starting the merging process. The main receiving branch should be determined beforehand. Also, remote changes should be updated in both receiving and merging branches.

To merge two branches, users first need to go to the base branch. By simply running the command ‘Git merge new branch’, the new branch is included in the master branch. After doing this, the interface will ask the commit message, which is ‘:wq’. This will save the commit message, and merging would finally be complete. To see changes, ‘git log‘ must be typed to show all the new codes added to previous ones.

Fast forward merge

This occurs when there is a linear path between the current and main branches. The branches can be combined automatically as commits from the main branch are now present in the current one. This is not possible in a three-way merge because of specific commits binding their histories. Many developers use fast-forward merging strategies to fix minor errors through rebasing. Three-way merges are used for lengthy features.

Git push

Simply running ‘git status‘ wouldn’t register the new branch and might not show how many branches there are in the local environment. This is where git push is used.

This simply means that users are pushing the currently active branch. Users can also push branches directly to the origin using the code ‘git push –set-upstream origin beta‘ (i.e., the new branch name). This command will now update the new branch created on the repository. Users can then switch to the new branch and see or create new codes within it.

Git push is used only when the local branch is already present. However, to get the list of all branches from the remote repository, the ‘git pull‘ command has to be run.

Deleting a branch

For deleting a branch, the command ‘git branch -d branch name‘ has to be run. For unmerged branches, instead of -d, -D (capital) can be used. To remove a remote branch, ‘git push origin –delete branch-name has to be run.

Conclusion

Creating and deleting branches in Git is more accessible than other VCS tools, as mentioned before. This is because git files are simple and have 40-character SHA-1, which is cheap to create. Earlier, VCS tools included copying project files into another directory which required a lot of time. However, in Git, the process is quick.

Functions like the git checkout help navigation of independent branches. It updates the versions in the working directory by recording new commits to the branch. A new branch for a new feature can be used in Git, which makes experimenting easier. These features provide a flexible workforce and encourage developers to use branches often.

Leave a Comment