234

I am looking for the command for creating a patch from the last commit made.

My workflow sometimes looks like this:

vi some.txt
git add some.txt
git commit -m "some change"

Now I just want to write:

git create-patch-from-last-commit-to-file SOME-PATCH0001.patch

What should I put there instead of create-patch-from-last-commit-to-file?

3

6 Answers 6

359

In general,

git format-patch -n HEAD^

(check help for the many options), although it's really for mailing them. For a single commit just

git show HEAD > some-patch0001.patch

will give you a useable patch.

11
  • 19
    be carefull with "git show HEAD > some-patch0001.patch", if it'S called in colored terminal it dups also color escape sequences into file.
    – hrach
    Apr 21, 2013 at 10:34
  • 14
    You can also use git diff > change.patch. You can include a revision range as well and it allows you to create patches for uncommitted changes. The big difference, however, is that it will not include differences in binary files. See the answer to What is the difference between 'git format-patch' and 'git diff'? for more detail.
    – Rangi Keen
    Dec 27, 2013 at 15:34
  • 4
    @hrach not on Linux it doesn't Feb 22, 2014 at 12:09
  • 4
    No, because that isn't what the first line says. Either git format-patch -1 or git format-patch -n HEAD^ should work.
    – Useless
    Jan 14, 2016 at 14:37
  • 5
    Thanks, I had not understood that it was literally the letter 'n', I thought it was a placeholder (format-patch has both '-n' and '-<n>' options).
    – Étienne
    Jan 18, 2016 at 21:51
72

Taking from @Useless answer, you can also use the general form with no parameters for the last commit and put it into a file with:

git format-patch HEAD^ --stdout > patchfile.patch

Or, being cleaner for windows users when carets have to be escaped by doubling them:

git format-patch HEAD~1 --stdout > patchfile.patch
3
  • 3
    Worked for me - thanks. If you happen to use Windows and Git, you have to escape the carrot (I know heinous): "git format-patch HEAD^^ --stdout > patchfile.patch" Aug 27, 2013 at 17:59
  • 3
    To avoid the Windows problem of having to escape that caret (which makes it look like a different valid git command), you can use the alternative of git format-patch HEAD~1. I think that ends up being less confusing on Windows. Jan 14, 2016 at 22:53
  • 1
    you can also use -o flag instead of --stdout. git format-patch HEAD^ -o ./patchfile.patch Apr 13, 2022 at 14:28
34

another way, if have the commit id of that particular commit, you can use,

git format-patch -1 {commit-id}
18
git format-patch -1

Does the job for me.

12

You need the -p option to git log:

git log -1 -p --pretty='%b'
1
  • 1
    Use this one if you don't care for the email from/date/subject lines at the beginning of the file.
    – Ryan H.
    Aug 16, 2017 at 23:47
1

For example if you are pushing code in branch "branch_name" on Github. Every commit on this branch will have separate url. Click on latest commit.

  1. Add .patch at the end of this url . So the modified url looks like: https://github.com/xyz/lmn-ms/tree/branch_name.patch.
  2. Then copy and paste the entire content which will come in step1 in separate local file and save it with .patch extention.
  3. Patch is ready to use.
1
  • This helped me quickly generate a patch from a single commit when git was picking up thousands of extra commits due to unrelated history w.r.t. current branch Dec 4, 2023 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.