create git patch from previous commit

WebMaster's picture

Best solution is

git diff MyRevisionNumber^! --relative > my.patch


  • git diff 15dc8^! as described in the following fragment of git-rev-parse(1) manpage (or in modern git gitrevisions(7) manpage): Two other shorthands for naming a set that is formed by a commit and its parent commits exist. The r1^@ notation means all parents of r1. r1^! includes commit r1 but excludes all of its parents. This means that you can use 15dc8^! as a shorthand for 15dc8^..15dc8 anywhere in git where revisions are needed. For diff command the git diff 15dc8^..15dc8 is understood as git diff 15dc8^ 15dc8, which means the difference between parent of commit (15dc8^) and commit (15dc8). via
  • --relative creates relative paths in patch, use it from the current working directory
  • the above solution is a shortcut for
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.
Drupal documentation wiki style