Create a patch including specific files in git

vasilakisfil picture vasilakisfil · Mar 30, 2012 · Viewed 28.7k times · Source

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?

Answer

Mark Longair picture Mark Longair · Mar 30, 2012

You can restrict 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

... to generate a patch that only shows the differences within a particular file and a particular directory.