git
patch
revisions
version control
tutorial

In git, how do I create a single patch for the last 2 revisions?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Git, "single patch for the last 2 revisions" can mean two slightly different things. You might want one combined diff representing the net effect of the last two commits, or one patch file that contains two commit-formatted patches in sequence.

Combined diff patch: one file, one net change

If you want a single patch that represents the total difference between HEAD~2 and HEAD, use git diff.

bash
git diff HEAD~2 HEAD > last-two.patch

That creates one plain diff file. It does not preserve the individual commit boundaries or metadata such as commit messages. It only captures the cumulative code change.

This is usually the right answer when you want to apply the final result elsewhere without caring about the original two-commit structure.

You can inspect it with:

bash
less last-two.patch

And apply it with:

bash
git apply last-two.patch

Commit-style patch stream: one file, two commits serialized

If you want to preserve the commit information but still write everything into one file, use git format-patch --stdout.

bash
git format-patch --stdout -2 > last-two-commits.patch

This produces one output file, but inside that file are two mailbox-style patches, one for each commit.

That is useful when you care about author, subject, and per-commit review context. It can be applied with:

bash
git am < last-two-commits.patch

The important distinction is:

  • 'git diff gives one cumulative diff'
  • 'git format-patch --stdout -2 gives a single file containing two commit patches'

How to choose between them

Use git diff HEAD~2 HEAD when:

  • you only care about the final combined change
  • you want a compact patch for manual review or quick application
  • preserving separate commit metadata is not important

Use git format-patch --stdout -2 when:

  • you want to replay the original commits
  • commit messages and authorship matter
  • the patch is going into an email or review flow that expects commit-formatted patches

Verifying the range before generating the patch

Before creating the patch, inspect the last two commits so you know the range is correct.

bash
git log --oneline -2

If you want a different span, adjust the starting revision accordingly. For example, the last three commits would use HEAD~3 HEAD for a combined diff or -3 for format-patch.

Applying the patch safely

For a plain diff patch:

bash
git apply --check last-two.patch

That validates whether the patch applies cleanly without modifying files.

For commit-formatted patches:

bash
git am --signoff < last-two-commits.patch

Use this only if you want actual commits recreated from the patch stream.

Common Pitfalls

The biggest mistake is saying "single patch" without clarifying whether commit history should be preserved. Git supports both interpretations, but they are not interchangeable.

Another issue is using git apply on a format-patch output when what you really want is git am. The commands operate on related but different patch formats and workflows.

It is also easy to choose the wrong revision range. HEAD~2..HEAD and HEAD~2 HEAD refer to the same diff span in this context, but you should still verify the commits before exporting anything.

Finally, remember that a combined diff loses per-commit messages and boundaries. If those matter later, generate the commit-style patch instead.

Summary

  • Use git diff HEAD~2 HEAD > last-two.patch for one combined diff.
  • Use git format-patch --stdout -2 > last-two-commits.patch for one file containing two commit patches.
  • 'git apply is for plain diff patches; git am is for commit-formatted patches.'
  • Check git log --oneline -2 first so you export the right commits.
  • The right command depends on whether you care about net change or original commit structure.

Course illustration
Course illustration

All Rights Reserved.