225

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?

2240
  • 1,547
  • 2
  • 12
  • 30
claj
  • 5,172
  • 2
  • 27
  • 30
  • possible duplicate of [Generate a git patch for a specific commit](http://stackoverflow.com/questions/6658313/generate-a-git-patch-for-a-specific-commit) – manojlds Feb 22 '12 at 17:19
  • 2
    A good source: https://ariejan.net/2009/10/26/how-to-create-and-apply-a-patch-with-git/ – Adam Ocsvari Sep 04 '15 at 10:10
  • 1
    Required preliminary setup info (my own Q&A): [How to configure and use `git send-email` to work with gmail to email patches to developers](https://stackoverflow.com/a/68238913/4561887) – Gabriel Staples Jul 03 '21 at 18:47

6 Answers6

349

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.

Useless
  • 64,155
  • 6
  • 88
  • 132
  • this one i shorter, which is good. git show HEAD > SOME-PATCH0001.patch would solve my problem, thank you! – claj Feb 22 '12 at 14:13
  • 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 '13 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'?](http://stackoverflow.com/a/4624292/2506021) for more detail. – Rangi Keen Dec 27 '13 at 15:34
  • 4
    @hrach not on Linux it doesn't – Robin Green Feb 22 '14 at 12:09
  • Isn't the first line wrong? For the last commit, it should be HEAD and not HEAD^. (git format-patch -1 HEAD^ will NOT generate a patch for the last commit). – Étienne Jan 14 '16 at 13:45
  • 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 '16 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 '-' options). – Étienne Jan 18 '16 at 21:51
  • @Useless can you please be kind enough to explain me what it is the purpose of using the `-n` sign, I read the Doc but couldn't get a clear idea, can please explain it in simple forms, as I am new to git – Kasun Siyambalapitiya Dec 14 '16 at 06:32
  • Try it (with and without `-n`) and see, it won't break anything. It just affects how the patch file is named. – Useless Dec 14 '16 at 10:53
  • `git show HEAD` and `git diff` generated different results @RangiKeen – alper Oct 01 '19 at 13:37
  • 2
    @alper You'd use `git diff HEAD~..HEAD` to show the differences in only the last commit. The only difference between that and `git show HEAD` is that the latter includes the commit information at the beginning. – Rangi Keen Oct 02 '19 at 13:31
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
a1an
  • 3,526
  • 5
  • 37
  • 57
  • 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" – Steve Midgley Aug 27 '13 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. – Michael Burr Jan 14 '16 at 22:53
  • 1
    you can also use -o flag instead of --stdout. `git format-patch HEAD^ -o ./patchfile.patch` – OMKAR AGRAWAL Apr 13 '22 at 14:28
34

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

git format-patch -1 {commit-id}
Vijay C
  • 4,739
  • 4
  • 41
  • 46
17
git format-patch -1

Does the job for me.

Katu
  • 1,296
  • 1
  • 24
  • 38
12

You need the -p option to git log:

git log -1 -p --pretty='%b'
Brandan
  • 14,735
  • 3
  • 56
  • 71
  • 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 '17 at 23:47
0

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.
Abdullah Imran
  • 259
  • 3
  • 13