Whenever, we have to create a new feature, we don’t directly make a change to the code in local and push it to the main branch, also called as the ‘master’ branch.
Rather, we create a new branch from the main branch, perform some changes e.g. adding or updating some feature and the submit our changes into our branch.
Later, on the development stage, if the code changes looks good, in most cases if reviewed by other peers, we raise a “Pull Request” to the master branch, often called as “PR” in short form, by which the code finally gets merged into the master branch.
This is the safe way, as we don’t alter any files of the main branch, rather perform changes in our branch and when everything seems to be OK and working we assemble our code to the master.
The above diagram represents how, the branching works.
We create two branches namely Branch A, Branch B to add feature A, feature B respectively out of the master branch.
After the feature has been added to the branches, we open a pull request to master branch.
If everything seems to be correct, A pull request is merged, and the branch is deleted.
When we clone a repository using git clone option, the repository is pointed at the main branch or the master branch. We also have flexibility to name our master branch as something else too.
E.g. some company may want to keep the name of it as “live” branch.
that really depends, but generally “master” is the name of the main branch.
When you enter the below command, it signifies what branch are you currently working with
$ git branch
To create a new branch out of “master” branch, make sure we are on master branch and put in the command below.
$ git checkout -b A
Now we have checked into new branch, called as branch A.
We can now start making our code changes in this branch, and after the changes are done.
We push the changes to remote, so that a new branch with “A” gets created in the remote as well.
Remember the git add . and git commit command from previous tutorial.
To push the changes simply, key in below command
$ git push origin A
The below command says, to push the changes to “origin” which is the Git URL, where we cloned our repo from.
Origin has been discussed in the earlier sections as well.
A is the branch name, where we want our changes to be pushed.
In this Tutorial we have understood, how to create a branch out of the main branch.
Perform our code changes according to the feature we are working on, and how can we later raise it for PR.