In this tutorial, we will focus on Git branching. In one of my previous tutorial, I have said that our default working directory works on the master branch.
Branching is an awesome feature of Version Control System. Consider when you work with a project and if you will face some issues, it is quite easy to fix issues by using Git branches. Another example is when you work on a project and it is going great, but you add a new feature to your project. You don't really want to mess with your current code. What you can do is create a new branch and add the new feature without messing the main source code.
Branching means, you separate from main line (master branch) of development and continue to do work without disturbing that main line. Most of the Version Control System has branching support.
You can work with the main line and any number of branches simultaneously. If you have some issue in your project and you don't need to interfere with your project, create a new branch and fix the issue, then add (merge) the fixed issue to your master branch.
Creating a new branch:
To create a new branch using the $ git branch <branch name> command.
Now you have created a new branch. But you are still in the master branch. Type $ git branch command.
The ' * ' sign implies the branch you are on. Go ahead and type $ git checkout <branch name>.
Now you are on your new branch. Let's create a new file (file3) and commit it. After you have successfully committed it check the Git log. You will notice that your latest commit is there.
Let's got the master branch and check the Git log. You will notice that your latest commit is disappeared.
The reason is that commit belongs to your new branch. But when you switch to the master branch it will not appear to you. But the commit is still there.
Let's try another example about branching. First, switch to the new branch and change the content of some file.
Then commit it. When you have committed successfully committed it, switch to the master branch. But the modification of your file is disappeared. This case is also same as the above scenario.
Delete a branch:
If you want to delete a branch, use $ git branch -D <branch name>. But think twice before you delete a branch. Before you delete the branch you want, switch to the master branch. Because you cannot delete the branch which you are currently on.
If you want to create a branch and switch to it in one command, use $ git branch -b <branch name>.
elaaa
ReplyDelete