Git : Branching and Merging

Branching is one of the most common features of any version control system is branching. A branch represents an independent line of development. The ease in which you can manage the branches in Git is one of reason for its wide popularity. Lets starts from where we left off in the previous post.

gitbasics_branch_0

As we can see, we are currently on branch master. The default branch name in Git is master. The master branch in Git is not a special branch. It is exactly like any other branch. Conventionaly, its the branch where your final code is.

Lets create a new branch by using $git branch <branch name> command

gitbasics_branch_1

To check which the list of branches, we used just $ git branch which shows the branches there with a astrix sign on what branch are we currently on. Now to switch to the newly brach created we use $ git checkout <branchname>

gitbasics_branch_2

Lets add another file to this feature1 branch.

file2AddedInFeaturesBranch

gitbasics_branch_3

Notice that after adding the commit in feature1 branch we switch to master branch and then checked the git log where no history of that commit is present.

Before we go on to merging these branches lets, take a look into another way to create a branch ($git checkout -b <branchname>), and then how to delete a branch ($git branch -D <branchname>).

gitbasics_branch_4

Now that we have learned to branch our repository, at some point of time we will require to merge it with our main branch. Merging not only combine the code in 2 branches but it also combines all of their respective commits into one timeline. There will be ocasions when the there will be a merge conflict, especially one changes are done in both branches on the same file. Git will resolve most of these conflicts by itself but on some occasions when the changes are done on the same line on both branches, Git will ask you to resolve it. We will see all in the following example.

Previously we had made a features1 branch and added a file2.txt to it. Lets merge that to our master branch.

gitbasics_merge_0

As you can see, the file added in features1 branch got into master branch along with the history of both the branches. This was easy, but there wasnt any merge conflict. Remember, a merge conflict will occur whenever there are changes in a common file in both branches. Lets change in file2.txt in master branch.

file2changedInMaster

Lets change in file2.txt in features1 branch, but on a different line, than the one done in master branch.

file2changedInFeatures1

Now lets switch to master and merge the features1 branch.

file2MergedInMasterAfterChangingInDiffLines

Notice that git automatically merged the file, without any conflicts even when the changes were done on the same file. Lets now change file1.txt and not we will change in the same line on both the branches. Lets do the changes in master first.

file1changedInMaster

Lets change the same line in features1 branch.

file1changedInFeatures1

Now , switch to master and try to merge.

file1-MergeConflict-ToBeSolvedManually

Notice that the file was merged with an message that there is a Merge Conflict in the file1 which needs to handled manually. Doing a git status tells us that the file is currently unstaged. If you open the file you will see the conflict area. A conflict-marked area begins with <<<<<<< and ends with >>>>>>>. These are also known as the conflict markers. The two conflicting blocks themselves are divided by a =======. You have many options here. You can choose HEAD change and delete the other ones, or keep the features1 change. You can delete both, keep both or have a completely new entry here. But make sure you resolve the marked area before commiting anything. Lets keep the changes of the features1 branch and remove the master one.

file1-MergeConflict-SolvedManually

Thats it for now. We will look into more concepts in future posts.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: