Everything About Git Part: 6

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 one 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:

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:

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 was required to commit commit ID, now we can check-out with the name of the branch. You need to use the following command to check out the branch.

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:

git checkout -b showTableNew

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 our project’s test.php file is in this state:

echo "Hello This is my First Git Learn";
Now we will display that newStudents variable in the form of a table as below.
echo "Hello This is my First Git Learn";
<table border="1" width="200" cellpadding="5" cellspacing="0">
<th>Students Name</th>
foreach($newStudents as $student){
echo "<tr><td><center>$student</center></td></tr>";
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:
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
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
git merge showTable

Show the difference between multiple committees

In Git, if you want, you can see the difference between one of your committees and any other committee, that is, the code of your previous committee has changed, where the code has been added, where it has been deleted, you can see all these with the help of the following Git command:

git commit compareFromId compareAgainstID