Everything About Git Part: 5

What is Branching and Merging at Git?

To understand what branching and merging in Git is, let me give you a real work experience: When we go to create a simple web site. It is often seen that a few developers have to work together as a team on a web site. Maybe someone in the team works with the home page. Another may work with about pages. It can be seen that one of the developers of the team works on one feature after another. And from this came the idea of ​​Branch. That is, one person works in one branch. Again, when the work of a feature is complete, everything has to be added to the original project again. And it is called Merge. When two developers are in two branches, no one sees anyone’s code. And after finishing the work, the two branches can be merged into one branch.

Creating a new branch:

We will now create a new branch called showTable, for which you need to type the following command:

1
git branch showTable

Now a branch named showTable will be created. The branch from which you created this new branch will have that version in the new branch. Since we have created showTable branch from master branch here. And so showTable will go to the version of the project that we currently have in the master branch. This means that now the project of master and showTable is the same.

With the following command you can see the list of all the branches in your project:

1
git branch

Branch checkout:

The brunch is ready, now we will checkout the brunch. Usually you can see which branch you are in by looking at the current working directory on your command line. Since we are still in the master branch in our project. And checking out the branch is a lot like checking out the committee. The difference is that before committing commit, we used to use commit ID, now we can check-out with the name of the branch. You need to use the following command to checkout the branch.

1
git checkout showTable

However, if you want to create a new branch and check-out at that branch immediately, you need to enter the following command:

1
git checkout -b showTableNew

Notice that we have created a new branch called showTableNew and immediately checked-out in that branch.

Now we will modify something in showTable and then merge it with master. And for this we have to go to our showTable branch with checkout showTable. Before that you have to take care of which branch you are in. For this you can see on the right side of the current working directory which branch you are in.

Branch modification

Now we will make some modifications in our showTable branch. Currently, the test.php file of our project is in this state:

<?php
$newStudents=["Safan","Shail","Tortoiz"];
echo "Hello This is my First Git Learn";
?>
Now we will display that newStudents variable in the form of a table as below.
<?php
$newStudents=["Safan","Shail","Tortoiz"];
echo "Hello This is my First Git Learn";
?>
<table border="1" width="200" cellpadding="5" cellspacing="0">
    <tr>
        <th>Students Name</th>
    </tr>
    <?php
    foreach($newStudents as $student){
        echo "<tr><td><center>$student</center></td></tr>";
    }
    ?>
</table>
Now I want to take this modified branch to my master branch or main project. But before that you have to commit these changes to the current branch i.e. showTable branch. Because as long as you don’t commit anything, Git won’t count them. So let’s commit:
1
2
git add --all
git commit -m "Tabulized Version Added"

Commit’s work is over. Now I will take the work in this showTable branch to the main master branch. That’s why you need to check-out at the master branch first

1
git checkout master

Now after checking out the master, you will see that the master is in the previous version. Now we will merge the modifications made in showTable with master branch. And for this, if you give this command while you are in the master branch, it will be merged automatically

1
git merge showTable