Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To turn an earlier local Git change into a patch, first find the commit in your repository’s reflog or history, then choose the right workflow: use git format-patch and git am to recreate commits, or use a diff and git apply to change files without creating commits.

Find the earlier change

A reflog can help locate commits that are no longer at the tip of a branch. It records local movements of branch and other reference tips; the HEAD reflog also records branch switching. A reflog entry is not a patch: use the commit it identifies as the basis for exporting the change.

git reflog

Inspect the relevant commit and your working tree before generating a patch. Confirm which changes you want and which branch or commit should serve as the base. A patch generated from the wrong endpoint can include more or fewer changes than intended.

Choose whether you need file changes or recreated commits

Command Result Use it when
git apply Applies a diff to working-tree files, or to the index with --cached; it does not create a commit. You want the changes as files or staged changes and will decide separately whether and when to commit.
git am Applies mailbox-style patches, typically produced by git format-patch, and creates commits. You want to recreate the patch series as commits, retaining the message and author information carried in the patch.

The Git project’s git-apply documentation puts the distinction plainly: “This command applies the patch but does not create a commit.” The git-format-patch documentation describes its output as “each non-merge commit with its ‘patch’ in one ‘message’ per commit, formatted to resemble a UNIX mailbox.”

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Create a patch series that preserves commits

Use git format-patch for non-merge commits when you want to create mailbox-style patch files that can be applied with git am. The revision arguments determine which commits are selected, so choose them deliberately.

Export commits after a known base

To export commits after a base commit through the current branch tip, use the base as the single <since> argument:

git format-patch <base-commit>

For example, if main is the base and the current branch contains the changes to export:

git format-patch main

This selects commits leading to the current tip that are absent from the history named by main. For exact selection, use a revision range and check the range semantics in the command documentation before exporting. When formatting from the beginning of history, --root changes how Git handles the root commit.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Apply the exported patches as commits

From the repository and branch where you want the commits added, apply the generated mailbox patches with git am. For example, if the patch files are in a directory named patches:

git am patches/*.patch

Each patch is intended to become a commit. The resulting commit history may not be identical in every circumstance, and application can fail if the target does not match the patch’s expected context or if the patch text was corrupted in transit.

Create and apply a diff without creating commits

If you only need file changes, create a diff with git diff and apply it with git apply. For example, to save the difference between two commits and check whether it applies to the current tree:

git diff <base-commit> <target-commit> > change.patch
git apply --check change.patch

If the check succeeds, apply the diff:

git apply change.patch

To apply it to the index without changing the working-tree files, use:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
git apply --cached change.patch

git apply --check tests applicability without applying the patch. The command can also apply a diff to files outside a Git repository when no index option is used, although a repository is generally more useful for reviewing and managing the change.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Reorder, edit, or remove patches

If you need to rearrange a series before applying it, the Git user manual documents a workflow of generating patches, resetting to a base, editing, reordering, or deleting patch files, and then applying them with git am. Interactive rebase is another option for modifying commit order or content.

Be careful with reset operations: depending on the command and state of the working tree, they can discard changes. Read the steps in the Git user manual and protect any uncommitted work before resetting.

Troubleshoot a patch that will not apply

  • Check the base: the target tree may differ from the one the patch was made against. Confirm that you are applying it to the expected branch or commit.
  • Check the patch text: if it was copied or sent through another system, verify that the text was not damaged in transit.
  • Preview a plain diff first: run git apply --check before applying it to see whether it fits the current tree.
  • Use partial rejection only when intended: by default, git apply applies a patch all-or-nothing when some hunks fail. With --reject, Git applies matching hunks and writes rejected portions to .rej files for manual resolution.

For command details that matter to a particular installation, consult the documentation matching your installed Git version; the Git project’s online manuals can change as releases evolve.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.