Pages

Sunday, July 20, 2014

Git - Branches and Conflict Merge

While working on a project, you do not want your uncompleted task code to cause a state where the project is not in the executable state. To overcome these situations you need project branches. Whenever you start a new task you create a new branch and write all your code to that branch and once you are done with task implementation and testing you merged that branch to the main branch and all developers now can access it.

To create branches and merging branches in Git lets see a workflow which we see while coding in our project. You’ll follow these steps:
  • You are working on a website.
  • Create a branch for a new issue/story.
  • Started implemented that issue fixes or story.

At this point, testing team found a critical issue on production and you must need to fix that issue at priority. Now you will:
  • Switch to Master/Production branch.
  • Create a branch to add the Patch.
  • After it’s tested, merge the Patch branch, and push to master branch.
  • Switch back to your original story and continue coding.

Git Branching


Initially, you are on the master branch where you already have some code commits.

You have started working on a new Story. You want to isolate your code from the master branch until the story is completely implemented and tested. So you will create a new branch for this story. To create a branch and switch to it at the same time, you can run the git checkout command with the -b switch:

$ git checkout -b story12
Switched to a new branch 'story12'
This is shorthand for:
$ git branch story12
$ git checkout story12
Now you work on the story and make changes to some files.
$ git add folder/file.html folder/file2.html
$ git commit -m 'added some new pages for story 12'

Now at this point testing team found a vulnerability in the website which needs to be fixed on urgent basis.You don't want to revert all your code you have written for Story 12 so now all you have to do is switch to master branch. But before switching to the master branch you need to commit or stash all your work otherwise, Git would not let you switch the branches until your current branch is not in a clean working state.
$ git checkout master
Switched to branch 'master'
Now you are at a point from where you started coding for story 12, so there is no extra code which is different from the code which is in production. So now you can create a new branch for Patch on which to code for the fix of vulnerability until it's completed and tested.
$ git checkout -b patch
Switched to a new branch 'patch'
$ vim design.html
$ git commit -a -m 'fixed the vulnerability'
Now when you are sure that fix has been completely implemented for the issues, you can merge your patch branch to master branch. You do this with the git merge command:
$ git checkout master
$ git merge patch
Now after complete testing you can deploy master branch to production.

Now when you are done with Patch fix and it's successfully deployed, you can switch back to your story which was incomplete. Here as you are done with Patch branch and you are sure that you won't need it now. You may delete it from Git. To delete it use the -d option to git branch:
$ git branch -d patch
Deleted branch patch (was 9d0574w).
Now switch back to your story.
$ git checkout Story12
Switched to branch 'Story12'

Here in Story12 branch, it has not the code you did for Patch branch. Now there are two options either you pull the master branch and merge master branch to your story12 branch immediately or you can wait until completion of Story12. It depends on the project requirement.

Basic Merging



When your story implementation is completed and tested and ready to be merged into the master branch. To merge the branch you have to check out the branch you wish to merge into and then run the git merge command:
$ git checkout master
$ git merge Story12
Auto-merging README
Merge made by the 'recursive' strategy.
README | 1 +
1 file changed, 1 insertion(+)
Now that your work is merged in, you have no further need for the Story12 branch. You can delete it:
$ git branch -d Story12

Merge Conflicts


Sometimes while merging there come a situation when two different branches have changed the same part of a file. In that case, Git shows the conflict in files where Git would not be able to merge them cleanly. If your Story12 branch and Patch branch have changed the same part of files then Git will prompt you a conflict:
$ git merge Story12
Auto-merging file.html
CONFLICT (content): Merge conflict in file.html
Automatic merge failed; fix conflicts and then commit the result.
Git won't automatically commit a new merge in conflict case. Here you need to resolve the conflicts of the files. If you want to see the files which are unmerged due to conflict, you can see it by:
$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")

Unmerged paths:
(use "git add ..." to mark resolution)

both modified: file.html

no changes added to commit (use "git add" and/or "git commit -a")

Git add the conflicts marker to files which has conflict like this:

<<<<<<< HEAD
Here is content
 =======
Here is updated contact
>>>>>>> Story12
The code between <<<<<HEAD to ======= is the code which was in master branch while code from ======== to >>>>>Story12 is code from Story12 branch. Here you have to decide which code you want to keep safe and which to remove and once you completed resolving conflict.

You can run git status again to verify that all conflicts have been resolved:
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)

modified: file.html
If you’re done with conflict resolution, you can add an commit the files:
$ git add file.html
$ git commit 
Now merge the branch:
Merge branch 'Story12'

Conflicts:
file.html
#
# It looks like you may be committing a merge.
# If this is not correct, please remove the file
#       .git/MERGE_HEAD
# and try again.
#
Git shows the default merge message you can edit and update with required details and information of merge.

Now you are done all of Story12 branch code, Patch branch and master branch is merged in the master branch.

No comments:

Post a Comment