Git Branch Concept and work on Git branch:
In general branch means separate workstation. Like this concept of separate workstation, we also can create separate branches in Git according to the steps of our flows like development(dev), Testing(test) or Production(prod) or based on what functionality we will add to our project.
We also create branches in Git for not to be messed up with our code. It’s a common practice to work in a group in any project. Many people work their own way. If they start to write code to same project in same branch and if one of them write wrong code, it may destroy the code structure or the entire project . So, for protecting the original(master branch), Git branching concept has come.
Some people write code as developer, some others write code for testing, etc. So, its best practice to make separate branches according to work styles. And finally “MERGE” these branches with the master(original branch).
Creating new branch:
When we create a repository, the default branch is called master. We can create additional branches :
git branch <branch_name>
$ git branch test
This just creates the new test branch, which at this point is exactly the same as our master (Original Branch) that means this branch gets property(I.g. : codes) exactly like master branch.
Checking available branches:
$ git branch
Two branches are available, test and master. Master is our default branch and marked with asterisk symbol. But if want work on our newly created branch, we have to switch to this branch.
Switching to another branch:
$ git checkout test
Now, we are in test branch, we can add new features here and update our code but it won’t affect master branch.
So, now one question should arise that how can master branch get updated??
Yes, we need to marge our branch with master branch. After adding new features we can commit and push it to our branch(test) and finally marge it with master branch.
If new features are successfully added to our branch, we can go back to the master branch.
$ git checkout master
Now marge dev branch with master branch
$git merge dev
Remove a branch:
The master branch is now up to date. The dev branch is no longer needed and can be removed.
$ git branch -d dev
Delete Local Branch:
To delete the local branch we can use one of the following:
$ git branch -d <branch-name> $ git branch -D <branch-name>
N.B.: The -d option is an alias for –delete, which only deletes the branch if it has already been
fully merged with its upstream branch.
We can also use -D, which is an alias for –delete –force, to delete the branch irrespective of its merged status.
Delete remote Branch:
$ git push --delete <remote-name> <branch-name>
N.B.: most of the cases remote-name is origin