GIT is the most used and powerful among the Version Control Systems. GIT branch is an important feature of the modern Version Control System. We can say branches in GIT is like a logical pointer of all the set of changes that you have made in the code base. In easy words, GIT branch is the local copy of the main codebase which will have the changes of the new feature or bug fix, which will later be merged with the main codebase once after verified. So, Let’s deep-dive into the GIT Branch its Operations.
Table of Contents
GIT Branching
GIT Branching is the strategy or systematic procedure of creating the branch and follow that up for merging that with the main copy of the main branch of the codebase. Before getting into the GIT branch, Let’s see why we need a branch.
Above Image shows how branching is done in GIT for developing a new feature. Basically, A branch is the isolated and independent line of developing the feature. Assume the middle line as the master branch where the code is stable and updated. Then, assume a developer-1 is assigned to develop Feature – 1. So, Developer-1 is cutting a branch from the master branch which is indicated in the above diagram as in Magenta color. And, Developer-2 is assigned to develop Feature-2 and he is cutting a branch from the master branch. And, that is indicated as in Blue color.
Let’s assume the codebase is the set of four files called File-1, File-2, File-3, and File-4. So, when Developer-1 is taking the branch from master to develop feature-1 (assume File-1 and File-3 are going to be changed for the development). So, the developer can safely commit his changes on both files into the master branch. Vice versa, When Developer-2 is taking the branch from master to develop feature-2 (assume File-2 and File-4 are going to be changed for the development).
Unlike other Version Control system, Branching in GIT is logically copying the codebase and when it is getting merged to master branch, the only snapshot of the changes is appended from the feature branch to the master branch. But in other Version Control System like SVN, Branches are literally copied while cutting the branch and replaced while merging with the master branch.
GIT Branch Operations and its Commands
As GIT Branch is about maintaining the separate line of development, it has multiple operations. Following are the GIT branch operations.
- Creating a New Branch
- Deleting a Branch
- Renaming the current Branch
- Other Basic Operations
Let’s see detailed explanation on these operations.
Creating a New Branch
To create a new branch from the master branch, pass the following command.
git branch <branch_name>
So, this will create a new branch but still you are not switched to the created new branch. So, to switch to the new branch, enter the following command
git checkout <branch_name >
So, GIT has another option to create and switch to the new branch at the same time by passing the following command
git checkout -b <branch_name>
Here the argument -b
is to create the branch along with checking out the created new branch.
Deleting a Branch
Just like a file or a folder, the branch can be deleted. To delete a branch, we have two different methods.
git branch -d <branch_name>
In this the argument ‘-d’ means deleting the branch only if the branch is pushed and merged with the remote branch. So, this is the safe delete option.
git branch -D <branch_name>
So, the argument -D
means deleting forcefully without checking whether the branch is pushed or not.
If you wish to delete the remote branch directly, pass the following command
git push origin --delete <branch_name>
Alternatively, we have short form of this command
git push origin :<branch_name>
Remember that origin
in the above command is the remote name of the GIT server
Renaming the current Branch
Renaming a branch is another important operation in GIT. Especially, when a developer creates a branch and push it to remote and wanted to rename.
To Rename local branch
If you want to rename the local branch and you are in the branch which you want to rename mean, Pass the following command
git branch -m <new_branch_name>
If you are in different branch and want to rename the branch locally mean, Pass the following command
git branch -m <old_branch_name> <new_branch_name>
To Rename the remote branch by deleting
If you want to delete the remote branch and push the new branch from local mean
git push origin :<old_branch_name> <new_branch_name>
This is again the deleting the branch but along with the same command, we are renaming the branch by appending new branch.
Rename the branch by resetting upstream
If you are in the branch which needs to be renamed in remote, then pass the following command with upstream argument -u
.
git push origin -u new-name
This will just rename the current upstream branch.
Other Basic Operations
Other than creating, deleting and renaming the GIT branch, we have a couple of other basic operations in the GIT branching
To list all local branch, Pass following command
git branch -a
If you want to List all remote branch, Use
git branch -r
for even more GIT operations, Check the documentation of GIT.
Conclusion
Among the Version Control Systems, GIT is having a different mechanism for branching. In this article, we have discussed what is GIT Branch and its Operations and how the commands of GIT Branch is been used. GIT can also be used as an automation tool if we can utilize and handle all the features of it. So, Let’s see more about GIT and its features in our upcoming article. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOps, Agile, DevSecOps and App Development.
Certified Cloud Automation Architect and DevSecOps expert, skilled in optimizing IT workflows with Six Sigma and Value Stream Management. Proficient in both technical and leadership roles, I deliver robust solutions and lead teams to success.
There are some points that I don’t understand in this article, can they be clarified for other articles?