How do I simply create a patch from my latest git commit?
GitPatchGit 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.
- Add .patch at the end of this url . So the modified url looks like: https://github.com/xyz/lmn-ms/tree/branch_name.patch.
- Then copy and paste the entire content which will come in step1 in separate local file and save it with .patch extention.
- Patch is ready to use.