claj
claj

Reputation: 5412

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

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?

Upvotes: 243

Views: 250130

Answers (6)

Useless
Useless

Reputation: 67802

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.

Upvotes: 372

Abdullah Imran
Abdullah Imran

Reputation: 399

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.

Upvotes: 1

Katu
Katu

Reputation: 1564

git format-patch -1

Does the job for me.

Upvotes: 21

a1an
a1an

Reputation: 3636

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

Upvotes: 74

Vijay C
Vijay C

Reputation: 4869

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

git format-patch -1 {commit-id}

Upvotes: 35

Brandan
Brandan

Reputation: 14983

You need the -p option to git log:

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

Upvotes: 12

Related Questions