What is a Branch in Git ?
Branch in Git is similar to the branch of a tree. A tree branch is attached to the central part of the tree called the trunk. While branches can generate and fall off, the trunk remains stand strong and is the only part by which we can say the tree is alive and standing. Similarly, a branch in Git is a way to keep developing and coding a new feature or modification to the software and still not affecting the main part of the project. We can also say that branches create another line of development in the project.The primary or default branch in Git is the master branch (similar to a trunk of the tree).
Working with Branches in Git -
Branch operation allows creating another line of development. We can use this operation to divide the development process into two different directions. For example, we developed a product for 1.0 version and we might want to create a branch so that the development of 2.0 features can be kept separate from 1.0 bug fixes.
Creating a new Branch :-
Suppose Akash creates a new branch using the git branch <branch name> command. We can create a new branch from an existing one. We can use a specific commit or tag as the starting point. If any specific commit ID is not provided, then the branch will be created with HEAD as its starting point.
git branch new_branchgit branch
Here the first command (git branch new_branch) will create a new branch and with the second command (git branch) i will get all the available branches , here Git shows an asterisk mark before currently checked out branch.
Deleting a Branch :-
A branch can be deleted by providing –D option with git branch command. But before deleting the existing branch, switch to the other branch . Suppose i’m currently on test_branch and i wants to remove that branch. So I switch branch and deletes branch as shown below.
git checkout master
Switched to branch 'master'
git branch -D test_branch
Deleted branch test_branch
with the first line (git branch) we are checking how many branches we have with this command we will get the list of available branches as we saw above after that with the second command (git checkout master) i’m checking out to another branch with this command you can checkout the branch you want to work on , here we are checking out to master branch after that now we have to delete a branch which is test_branch so to delete that branch we have to use third command (git branch -D test_branch) this command will delete the test_branch.
Rename a Branch :-
If in case you feel like the branch you are working on does not has a appropriate name you can change the branch name in order to changes the branch name use –m option followed by the old branch name and the new branch name.
new_branchgit branch -m new_branch testing_branch
Now, the git branch command will show the new branch name.