Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a patch including specific files in git

Tags:

git

file

patch

Let's say that I am 7 commits ahead of the origin/master repo. I would like to create a patch that includes in the patch specific files that were changed, not all the files. Or equivalent exclude specific files from the patch that were changed. How can I achieve that?

like image 343
vasilakisfil Avatar asked Mar 30 '12 09:03

vasilakisfil


People also ask

How do I create a patch for one file?

You can create a patch file by restricting the output of git diff by listing paths at the end of the command (after a -- to avoid any potential clashes between path names and branch names). Show activity on this post. Will give you three files 0001-[commit] ... 0003-[commit] only containing the Makefile.

What is a patch file in git?

Patch is a text file, whose contents are similar to Git diff, but along with code, it also has metadata about commits; e.g., commit ID, date, commit message, etc. We can create a patch from commits and other people can apply them to their repository. Jerry implements the strcat function for his project.


1 Answers

You can create a patch file by restricting the output of git diff by listing paths at the end of the command (after a -- to avoid any potential clashes between path names and branch names).

For example, you could do the following:

git diff origin/master HEAD -- app/models/region.rb doc/ > changes.patch 

Above commands generate a patch that shows only the differences for a particular file: region.rb and a particular directory : doc when compared to origin/master

Then you can apply the patch using

patch -p1 < changes.patch 

The -p1 tells patch to strip the a/ and b/ in the paths in the patch

like image 195
Mark Longair Avatar answered Nov 02 '22 14:11

Mark Longair