How can I generate a Git patch for a specific commit?

Question

I need to write a script that creates patches for a list of SHA-1 commit numbers.

I tried using git format-patch <the SHA1>, but that generated a patch for each commit since that SHA-1 value. After a few hundred patches were generated, I had to kill the process.

Is there a way to generate a patch only for the specific SHA-1 value?

Answer

Try:

git format-patch -1 <sha>

or

git format-patch -1 HEAD

According to the documentation link above, the -1 flag tells Git how many commits should be included in the patch;

-<n>

     Prepare patches from the topmost commits.


Apply the patch with the command:

git am < file.patch

Alternatively you can also apply (should work on all OSes including Windows) with:

git apply --verbose file.patch

The -v or --verbose will show what failed, if any. Giving you a clue on how to fix.

Completely cancel a rebase

How do I configure git to ignore some files locally?