Sunday 7 April 2013

How to create a patch in git

In this article, i am going to explain how to create a patch in Git Version control. Before look into that how to do that, we need to ask a question our self why we have to go with "patch" instead of merge option is available in git.

What a need of patch?

If you have master branch with bug free(just example), in later if you want to work with any enhancement/future kind of stuff you can create a new branch called "future". So once all enhancements are done you can merge "future" branch into "master" branch. For example in your future branch shared with multiple developers so obviously your "future" branch will have good amount of commits. So while you merge with "master" later, you can do it in two ways one is "merge" and another one is "patch"(create a patch and apply that patch into master branch). If you go with "merge" option, reverting back to previous version is little bit headache( because git will creates so many merge commits). suppose if you choose patch style, it will be single commit for your entire "future" branch changes. Lets see how to do it in git as command.

just assume you are in future branch and you have done all necessary changes on that branch, just we want to create a patch

git checkout future (if you want create a patch from "future" branch)
git diff master >> my_patch_name.patch

Now go to your master branch and then apply created patch from future branch.

git checkout master
git apply my_patch_name.patch
git add .
git commit -m"future branch merged with master"
git push origin master

It will create a single commit in master after you applied patch, just delete that patch file which the file has extention .patch. Good thing is if you want to revert back to your previous commit too easy.

Hopefully you can enjoy this article!!!.

No comments :

Post a Comment