How to create a patch in git
You can use “git format-patch” on a specific commit or list of commits to creating a patch. The patches get created in the current directory for each commit. Use the –stdout option to store the patch into a single file. You can use the -o option to keep the patches in a specific directory.
Let us see these scenarios through the hands-on example I did on an actual Ubuntu machine from an AWS EC2 instance from the cloud with a real GitHub account.
The initial setup is as shown. The repository repoone has a branch main. There are ten commits in the branch.
~/repoone$ git remote -v
origin git@github.com:codeversionmaster/repoone.git (fetch)
origin git@github.com:codeversionmaster/repoone.git (push)
~/repoone$ git branch
* main
~/repoone$ git log --oneline
fc44f72 (HEAD -> main) Sixth line in newfile
19d7f63 Update newfile
4ab2642 Fourth line added in newfile
bead66e Another change to new file
3f54558 This is new file
4632c0d Adding empty directory
908d6ac Third commit in main
42296d0 Second commit in main
453424e This is first commit
2673111 Initial commit
Creating a patch for a specific commit
To create a patch for a specific commit, use git format-patch -1 with that specific commit id as an argument. -1 means that you need patches for 1 commit starting from itself. For n commits, including the commit and below the commit, use -n. The patch file is created in the current directory by default.
~/repoone$ git format-patch -1 fc44f72
0001-Sixth-line-in-newfile.patch
Creating patches for a series of multiple commits
To create a patch for multiple commits that are in order, use git format-patch <starting commit id>^..<ending commit id>. Here, the ^ symbol indicates that the starting commit should also be included in its sequence. The patch files are created in the current directory by default.
~/repoone$ git format-patch 4ab2642^..fc44f72
0001-Fourth-line-added-in-newfile.patch
0002-Update-newfile.patch
0003-Sixth-line-in-newfile.patch
Creating a single patch file for multiple commits
To create a patch in a single file for multiple commits, use git format-patch <starting commit id>^..<ending commit id> –stdout. And redirect the command output to a single file, as shown.
Here, 3commits.patch will store all the patches of 3 commits. And we have done grep to see the subjects of each patch for confirmation in this example.
~/repoone$ git format-patch 4ab2642^..fc44f72 --stdout > 3commits.patch
~/repoone$ cat 3commits.patch | grep Subject
Subject: [PATCH 1/3] Fourth line added in newfile
Subject: [PATCH 2/3] Update newfile
Subject: [PATCH 3/3] Sixth line in newfile
Creating patch files in a specific directory
You can use the -o option on git format-patch to store the patch files in a specific directory instead of the current one.
In the example, patchdir is the directory that will store the patch files.
~/repoone$ git format-patch 4ab2642^..fc44f72 -o patchdir
patchdir/0001-Fourth-line-added-in-newfile.patch
patchdir/0002-Update-newfile.patch
patchdir/0003-Sixth-line-in-newfile.patch
You can also check out the article on how to apply a patch in git. The article will help use the above-created patch and apply it to the branch.