git format-patch HEAD^
This command will diff all commits from the penultimate commit HEAD^
to the last commit HEAD
and generate a patch file containing these differences. Here is an example with detailed steps:
The first step is to create a new git repository and make some commits. These commits are for which we will generate patches later.
mkdir testrepo
cd testrepo
git init
echo "Hello, world!" > hello.txt
git add hello.txt
git commit -m "First commit"
Now we have a hello.txt
file named "Hello, world!". Then we add one more change:
echo "Hello, everyone!" > hello.txt
git commit -am "Second commit"
Second step, now use to git format-patch HEAD^
generate the patch file:
git format-patch HEAD^
There should now be a file in the current directory 0001-Second-commit.patch
. This file is the patch file, which describes all the changes from the first submission to the second submission. You can view this patch file:
cat 0001-Second-commit.patch
This patch file can be applied to another git repository, or sent to other developers, who can use the patch to apply your changes to their repositories. To apply this patch, use git apply
the command:
git apply 0001-Second-commit.patch
Implement the need for custom patch file names
First git format-patch
let automatically generate the patch file, and then use mv
the command to rename the file:
git format-patch -1
mv 0001-Some-previous-commit.patch mypatch.patch