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!!!.
  

How to checkout other branch exact files in git

Here i am going to explain you, how to checkout other branch particular file in your working tree or current branch. Just assume your current working directory is "future"(git branch name is future).
You have one more branch called "master". Suppose if you want to checkout particular file from master branch into your future branch. Then do the following command it will work out well.

Lets move to your current branch, here "future" using following command

git checkout future

Now you shifted into "future" branch. Then do the following command, which we need exactly for our need(checkout particular file from master branch).

git checkout master -- file_1_name.txt,file_2_name.txt
(Just for examples file_1_name.txt and file_2_name.txt. According to your need you can change into your desired filenames list).

The above command will override your current branch(future branch) files you mentioned above command. So your local changes will be ignored.

Hopefully now you come to know how to checkout other branch files in your current branch!!.