Jay Taylor's notes

back to listing index

Generate a git patch for a specific commit

[web search]
Original source (stackoverflow.com)
Tags: git patch 3-way stackoverflow.com
Clipped on: 2016-10-08

I need to write a script that create patches for a list of SHA1 commit numbers.

I tried using git format-patch <the SHA1>, but that generated a patch for each commit since that SHA1. 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 SHA1?

asked Jul 12 '11 at 0:35
Image (Asset 3/9) alt=
elle
3,202384

Try:

git format-patch -1 <sha>
answered Jul 12 '11 at 0:43
Image (Asset 5/9) alt=
manojlds
153k26278306
127 upvote
  flag
Easiest: git format-patch -1 HEAD – Naftuli Tzvi Kay Mar 13 '13 at 3:40
91 upvote
  flag
Apply it with: patch -p1 < file.patch – Jorge Barata Dec 4 '13 at 12:17
74 upvote
  flag
Applying the patch: git apply --stat file.patch # show stats. git apply --check file.patch # check for error before applying. git am < file.patch # apply the patch finally. – Adrian Mar 25 '14 at 14:15
3 upvote
  flag
Even easier with 1.8.5 git format-patch -1 @ – Evan Purkhiser Jun 17 '14 at 8:09
4 upvote
  flag
Use git am -3 < file.patch to apply using a three-way merge which will let you resolve conflicts using git mergetool afterward (or editing manually) found here. – Matt Aug 5 '15 at 21:11

For generating the patches from the topmost commits from a specific sha1 hash:

git format-patch -<n> <SHA1>

The last 10 patches from head in a single patch file:

git format-patch -10 HEAD --stdout > 0001-last-10-commits.patch
answered Apr 23 '13 at 14:34
Image (Asset 6/9) alt=
Sriram Murali
2,13211220

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 hashes.

answered Jul 12 '11 at 0:42
Image (Asset 7/9) alt=
dookehster
741511
   upvote
  flag
Thank you dookehster for the reply. That means I need the script to find the commits that preceded those I am interested in. I was hoping that I could avoid that. – elle Jul 12 '11 at 0:45
7 upvote
  flag
@elle, no, you don't -- git diff hash^ hash . the "hash^" give the preceded commit. (but, of course, manojlds's answer is better) – J-16 SDiZ Jul 12 '11 at 0:52
1 upvote
  flag
git show HEAD > mypatch.diff while you're on the commit should do the same. – andho Apr 21 '15 at 12:49

This command (as suggested already by @Naftuli Tzvi Kay):

git format-patch -1 HEAD

Replace HEAD with specific hash or range.

will generate the patch file for the latest commit formatted to resemble UNIX mailbox format.

-<n> - Prepare patches from the topmost commits.

Then you can re-apply the patch file in a mailbox format by:

git am -3k 001*.patch

See: man git-format-patch.

answered Jul 28 at 13:11
community wiki

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)

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.

answered May 24 at 7:26
Image (Asset 8/9) alt=
VonC
625k19017961881

To generate path from a specific commit (not the last commit):

git format-patch -M -C COMMIT_VALUE~1..COMMIT_VALUE
answered May 4 at 16:52
Image (Asset 9/9) alt=
Makah
1,60611740

Your Answer

asked

5 years ago

viewed

245092 times

active

2 months ago

Blog

Featured on Meta

Hot Network Questions

Technology Life / Arts Culture / Recreation Science Other
  1. Stack Overflow
  2. Server Fault
  3. Super User
  4. Web Applications
  5. Ask Ubuntu
  6. Webmasters
  7. Game Development
  8. TeX - LaTeX
  1. Programmers
  2. Unix & Linux
  3. Ask Different (Apple)
  4. WordPress Development
  5. Geographic Information Systems
  6. Electrical Engineering
  7. Android Enthusiasts
  8. Information Security
  1. Database Administrators
  2. Drupal Answers
  3. SharePoint
  4. User Experience
  5. Mathematica
  6. Salesforce
  7. ExpressionEngine® Answers
  8. Cryptography
  1. Code Review
  2. Magento
  3. Signal Processing
  4. Raspberry Pi
  5. Programming Puzzles & Code Golf
  6. more (7)
  1. Photography
  2. Science Fiction & Fantasy
  3. Graphic Design
  4. Movies & TV
  5. Music: Practice & Theory
  6. Seasoned Advice (cooking)
  7. Home Improvement
  8. Personal Finance & Money
  1. Academia
  2. more (8)
  1. English Language & Usage
  2. Skeptics
  3. Mi Yodeya (Judaism)
  4. Travel
  5. Christianity
  6. English Language Learners
  7. Japanese Language
  8. Arqade (gaming)
  1. Bicycles
  2. Role-playing Games
  3. Anime & Manga
  4. more (18)
  1. Mathematics
  2. Cross Validated (stats)
  3. Theoretical Computer Science
  4. Physics
  5. MathOverflow
  6. Chemistry
  7. Biology
  8. Computer Science
  1. Philosophy
  2. more (3)
  1. Stack Apps
  2. Meta Stack Exchange
  3. Area 51
  4. Stack Overflow Careers
site design / logo © 2016 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required
rev 2016.10.8.4048