Branching: Branching is a new feature which is available in the modern version control system, The branch is used to store the history of commits.
Merging: Merging is nothing but combining the sequences of different commits into a unified commits history, is called as merging the branches.
Forking and Cloning with Git
Branching is a new feature which is available in the modern version control system, The branch is used to store the history of commits.
Git is having default branch called the master branch, we can create a new branch with an existing branch, we normally use a specific tag or commits as a starting point.
If we won't specify any starting point, then the branch is created with HEAD as its starting point. Where every branch is referenced by HEAD, which points to the latest commit in the branch.
Whenever you make a commit, HEAD is updated with the latest commit, Usually, a branch is used to work on the new feature, once the feature is completed we will merge back to the master branch.
git statusbefore starting with the new project.
So, here we are creating a new feature branch and once the feature is completed, we are integrating back to the master branch.
To start off with the branches first, am going to list out all the branches which are present in our Git repository by using
git branch -a.
Where "-a" parameter will list out all the branches including both Local and the Remote branches and git branch will recognize the current active branch by putting an asterisk before the branch name.
To create a new branch, we will use the command
git branch <new branch name>, I am going to create a new branch called mynewbranch,
git branch mynewbranch
So if you pass the command
git branch -a, it will list the new branch, and the asterisk is still on the master branch only, so to switch that we will use the checkout command.
So, let's use
git checkout mynewbranch, it will switch branch to the mynewbranch, and then the asterisk will move on to mynewbranch.
Now you can see the whole history by using the command,
git log --oneline --decorate , If you observe, the first commit which is listed here is the last commit with several labels associated with it, like HEAD, which usually points to the last commit on the current branch.
And also we have origin/master, origin/head, mynewbranch, and the master branch, Here HEAD and origin/head points to the last commit on their respective branches in both Remote and Local.
Master branch is the branch where we are working with from long and mynewbranch is just now we had created Since we have not made any changes and hence all the branch labels are pointing to the same commit.
The branches are just pointers and new branch won't start without any particular commits other than from master.
Hence we are going back to the master branch by using the git checkout command as,
git checkout master
We can also rename the branch by using the command line,
git branch -m where "-m" move old branch name to the new branch name like from mynewbranch to newbranch
git branch -m mynewbranch newbranch, so now, mynewbranch has simply named as newbranch
And if we want to delete the branch which is of no longer used and also you cannot delete any branch which is currently you are on, before deleting, first move on to different one and then you can delete it.
git branch -d <branch name>
Here am going to delete newbranch,
git branch -d newbranch
To check whether it has removed or not, you can use the command,
git branch -a so, as I told you before, it will list all the existing branches.
Merging is nothing but combining the sequences of different commits into a unified commits history, is called as merging the branches.
The Merge command is used to integrate changes from another branch and combining all the integrated changes into a single commit, instead of treating them as individual commits.
There are two kinds of Merging:
This kind of merging happens only when the master branch has not diverged and there should be a linear path between the branches that we wanted to merge.
Let us consider an example: there two branches called Master branch another one is Fatty branch if the master branch is in the same direction without any diversion, then instead of creating a new commit, the master branch will point to the last commit of the Fatty branch, So now all the commits from Fatty branch are available in master branch.
When there is not any linear path between the two branches, then Git will join those two branches by using the extra commit, so this is called a three-way branch
We had learned the basic concepts in branching and now, let's do some actual branching, committing and merging.
git status, Git tells us that we are currently on a clean working directory followed by Master branch.
git branch, which will list Local branches.
git branch -awill list all the branches including the Remote branches.
git checkout -b title-change
git status, Git will tell you that, you are on the title-change branch with a clean working directory.
git status, Git shows that we are having a modified file.
git commit -am "Changing Title of File"and press enter.
git log --oneline, you can see your last commit right up there.
git checkout master
git diff master title-changeand press enter.
git difftool master title-changeso, here you can compare the differences.
Now, I can merge in my changes, type git merge and then the name of the branch you wanted to merge into the current branch
git merge title-change
And now, Git will respond as merging is successful and updating and also you can see the HASH code of the commits which are involved in the merging.
Git also tells that, It was a Fast Forward merge and the file which is involved is simple.html, and there was a "1 file changed, 1 insertion(+), and 1 deletion(-), the way that Git sees a change on the same line is a line deletion and line insertion at the same place called Fast Forward Merge.
This is because Git placed all the commits into the master branch as if we never branched away and also we have not done any additional work on the master branch before merging into master.
git log --oneline --graph --decorate , you can see that the HEAD is pointing to the last commit on the master branch and also to the title-change branch and master is currently pointing to that commit as well.
git branch, we have our master branch and title-change branch.
git branch -d title-change
git branch, thus we have our master branch only.
git log --oneline --graph --decorate, you can see that at the top commit, which is nothing but our last commit with only master and HEAD are pointing to that.
We know how to do a Fast Forward merging in Git, now, let us understand how to disable the Fast Forward Merge in Git and follow the below instructions:
git status, Git tells us that, we are on the master branch with a clean working directory.
git branch, we have only a Master branch in it.
git checkout -b add-copyrightand press enter.
git branch, you can see that the add-copyright branch has listed over here and also you can see an asterisk next to it, this means that the current working directory is add-copyright.
git status, we have modified file, so let's add this file to the staging area and commit the changes.
git commit -am "Adding copyright notice"and press enter.
git commit -am "Adding the copyright notice to the README.md file"
git log --oneline --graph --decorateyou can see the copyright notice at the top, which is nothing but our last commit, press "q" and exit.
git checkout master
git merge add-copyright --no-ff
git log --oneline --graph -- decorate, we can see that the graph lines being preserved.
git branch -d add-copyright.
git branch, the add-copyright branch has deleted.
git log --oneline --graph --decorate, you can see that the branching still occurs and we do not have the labels associated with it.
Till now we have learned how to do Fast Forward merge and how to disable the Fast Forward merging, now let's understand how to make Git do an Automatic merge and what is the command we need to use, come let's go through it.
In this part, there will be an automatic merge that will result in a merge commit automatically, The first thing is to, go to your project folder and then get into your Git repository.
Here I have my starter-web-1/ repository, just check for
git status, before moving forward. Git tells us that we are on the master branch with a clean working directory.
git branch, you can see only the Master branch.
git checkout -b Sample-changes
ls, so that, you can see some files over there.
Let's go ahead and edit some files in that separately, Start with a humans.txt file, open humans.txt by using the default editor and add some Team information details under the Team and save then exit.
git status, Git tells that we have a modified file and let's commit that file.
git commit -am "Adding team instructions to the humans.txt"
git checkout master.
While I am on my master branch, am going to edit or modify another file called README.md and this could be done by another developer or this could be done by making this change just by context switching or fixing something on the production.
Let's go ahead and edit the README file and add some text under the "How to Contribute" as "please fork Git repository and then issue the pull request for review" save and then exit.
git status, Git tells that, we have a modified file and let us commit that file.
git commit -am "Adding instructions on how to commit"
git log --oneline --graph --decorate --all, you can see that on the top, we have two commits, we have the commit on sample-changes that is different than the commit on the master branch.
git merge sample-changes -m "merging changes from sample-changes branch"where -m parameter will result in the commit message and in the double quotes representing our commit message.
If you check by using the git log command
git log --oneline --graph --decorate --all . You can see that the merge has happened and we have our commit message too, but the sample-changes is still preserved as a separate branch.
git branch -d sample-changes
In this section am going to cover, how to resolve merge conflicts, most of the time, we will end up doing Fast Forward merges, or
Merges that could result in Fast Forwards or else Git is going to merge automatically however, sometimes we are going to run into the conflicts and need to resolve them. Now let us follow as below.
git status, Git tells us that we are on master branch with a clean working directory with several commits ahead of origin.
git branchsince I have a master branch only.
git checkout-b realwork, Now do some changes in simple.html in the realwork branch and switch back to the master branch and do similar changes in the same area.
git status, Git will tell us that, we have modified file.
git commit -am "Making changes to simple.html"
git status, so our Git directory is clean on this branch.
git checkout master
And hence Git will change out all the files that are in the working directory to reflect the current state of the branch you are going to.
Hence I switched back and forth between different branches or different references within Git. Git is managing my working folder for me.
So let us make some additional changes that are going to create conflict and add the title as A very powerful website.
We have made some more changes to the simple.html file, and these changes should conflict the other changes I made in another branch.
git status, we have a modified file.
git add simple.html
git commitpress enter, the default editor page will open, enter your commit message over there save then exit.
git log --oneline --graph --decorate --all, so you can see that our work has diverged, and you can see that the realwork and master changes are different.
git difftoolwould be very helpful.
git diff master realwork, so that we can see the comparison between the two branches thus we can see some conflicts over here.
git difftool master realwork
git merge realwork
Here, the first thing we can observe that Git trying to merge simple.html automatically but it could not do that instead resulted with conflict in the content as Automatic merge failed.
We have to fix the conflict, here we are merging against the master, so we are in the merging state. Neither we do not have a clean working directory nor in the staged area, we are in between the merging.
Let's go ahead and have look at the simple.html, you will recognize differences, probably if you are familiar with the other source control tools that are popular in the Linux or Unix.
Then you may be comfortable to understand what Git will mark up your file to show the differences but Git will modify the file showing the differences.
Then go ahead and modify the file manually, earlier we set up the visual diff and mergetools so that we could do this with a graphical tool.
So, to invoke our graphical tool, to help resolve our merge conflict, type git mergetool, and that will launch the p4merge, P4Merge is a pretty nice three-way mergetool, that we can use to resolve the conflicts.
git commit -m "Done resolving the merge conflicts"
git status, you can see that I do have one untracked file and that's because during resolving the merge, Git will save off an original copy of the merge conflicts.
notepad++ .gitignoreso, this will capture any file that ends with .orig save then exit.
git status, now it is excluded the .orig.
git add .gitignore.
git commit -m "updating ignore file to exclude merge temp files"
git branch, I still have realwork branch here. since I have done merge with that branch and hence it is no longer required, so delete that branch
git branch -d realwork.
git log --oneline --graph --decorate --all, you can see that the "Done resolving merge conflicts" is my merge commit, followed up by the latest commit, which I needed to update the ignore file to exclude those temp files.
We have reached the end point of Branching and Merging, so let us clean up the working directory and push the changes back to the Github.
git status, you can see the clean working directory, with some commits ahead.
git branch, so here I have only the Master.
git pull origin master, as expected it is already up-to-date.
git push origin masterso, we have done everything here.