How do I simply create a patch from my latest git commit?

GitPatch

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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

Solution 3 - Git

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

git format-patch -1 {commit-id}

Solution 4 - Git

git format-patch -1

Does the job for me.

Solution 5 - Git

You need the -p option to git log:

git log -1 -p --pretty='%b'

Solution 6 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionclajView Question on Stackoverflow
Solution 1 - GitUselessView Answer on Stackoverflow
Solution 2 - Gita1anView Answer on Stackoverflow
Solution 3 - GitVijay CView Answer on Stackoverflow
Solution 4 - GitKatuView Answer on Stackoverflow
Solution 5 - GitBrandanView Answer on Stackoverflow
Solution 6 - GitAbdullah ImranView Answer on Stackoverflow