Files
Advanced-Git/contents/create-patch-from-last-commit-s.md
2025-07-11 03:55:14 +00:00

1.3 KiB

⬅️ Back to Sharing Changes as Patch Files

Create Patch from Last Commit(s)

Category: Patch & Diff

Command

git format-patch HEAD~1

Examples

  • Create a .patch file for the last commit.
git format-patch HEAD~1 
  • Create a single patch file for all commits on top of main.
git format-patch origin/main..HEAD --stdout > my-changes.patch 
  • Create patch files for the last two commits.
git format-patch -2 
  • Create patch files for all commits since main.
git format-patch -2 origin/main..HEAD 

Steps

  1. Run 'git format-patch HEAD~1' to create a patch for the last commit.
  2. Use 'git format-patch origin/main..HEAD --stdout > my-changes.patch' to create a single patch file for multiple commits.

Warnings

  • ⚠️ Patch files created this way include commit messages, authorship, and timestamps.
  • ⚠️ Use 'git am' to apply these patches on another system.

➡️ See the Next Step: Apply Patch with Commit Metadata


Author: mike-rambil • Updated: 2024-06-10 • Tags: patch, format-patch, committed