Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

patch

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?

like image 991
elle Avatar asked Jul 12 '11 00:07

elle


People also ask

How do you make a patch of a commit?

In order to create Git patch file for a specific commit, use the “git format-patch” command with the “-1” option and the commit SHA. In order to get the commit SHA, you have to use the “git log” command and look for the corresponding commit SHA.

What is a commit patch?

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.


1 Answers

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 
like image 131
manojlds Avatar answered Oct 16 '22 13:10

manojlds