How Can I Generate a Git Patch for a Specific Commit?
I Need to Write a Script That Creates Patches for a List of Sha-1 Commit Numbers. I Tried Using Git Format-Patch, but That Generated a Patch for Each Commit...
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?
11 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
For generating the patches from the topmost <n> commits from a specific SHA-1 hash:
git format-patch -<n> <SHA-1>
The last 10 patches from head in a single patch file:
git format-patch -10 HEAD --stdout > 0001-last-10-commits.patch
Say you have commit id 2 after commit 1 you would be able to run:
git diff 2 1 > mypatch.diff
where 2 and 1 are SHA-1 hashes.
This command (as suggested already by @Naftuli Tzvi Kay),
git format-patch -1 HEAD
Replace HEAD with a specific hash or range.
will generate the patch file for the latest commit formatted to resemble the Unix mailbox format.
-<n>- Prepare patches from the topmost <n> commits.
Then you can reapply the patch file in a mailbox format by:
git am -3k 001*.patch
See: man git-format-patch.
git format-patch commit_Id~1..commit_Id
git apply patch-file-name
Fast and simple solution.
If you want to be sure the (single commit) patch will be applied on top of a specific commit, you can use the new git 2.9 (June 2016) option git format-patch --base
git format-patch --base=COMMIT_VALUE~ -M -C COMMIT_VALUE~..COMMIT_VALUE
# or
git format-patch --base=auto -M -C COMMIT_VALUE~..COMMIT_VALUE
# or
git config format.useAutoBase true
git format-patch -M -C COMMIT_VALUE~..COMMIT_VALUE
See commit bb52995, commit 3de6651, commit fa2ab86, commit ded2c09 (26 Apr 2016) by Xiaolong Ye (``).
(Merged by Junio C Hamano -- gitster -- in commit 72ce3ff, 23 May 2016)
Must Read
format-patch: add '--base' option to record base tree info
Maintainers or third party testers may want to know the exact base tree the patch series applies to. Teach git format-patch a '
--base' option to record the base tree info and append it at the end of the first message (either the cover letter or the first patch in the series).The base tree info consists of the "base commit", which is a well-known commit that is part of the stable part of the project history everybody else works off of, and zero or more "prerequisite patches", which are well-known patches in flight that is not yet part of the "base commit" that need to be applied on top of "base commit" in topological order before the patches can be applied.
The "base commit" is shown as "
base-commit:" followed by the 40-hex of the commit object name.
A "prerequisite patch" is shown as "prerequisite-patch-id:" followed by the 40-hex "patch id", which can be obtained by passing the patch through the "git patch-id --stable" command.
Git 2.23 (Q3 2019) will improve that, because the "--base" option of "format-patch" computed the patch-ids for prerequisite patches in an unstable way, which has been updated to compute in a way that is compatible with "git patch-id --stable".
See commit a8f6855, commit 6f93d26 (26 Apr 2019) by Stephen Boyd (akshayka).
(Merged by Junio C Hamano -- gitster -- in commit 8202d12, 13 Jun 2019)