Command Reference

git-apply Tutorial

Explains how to use git-apply to apply patch content to the working tree or index.

Who This Is For
  • Developers who already know basic commit and branch actions
  • Readers who want to understand command boundaries and risk
Prerequisites
  • A basic mental model of worktree, index, and commits
  • Comfort reading `git status` and a small commit graph
Common Risks
  • Using local cleanup commands on already shared history
  • Continuing to rewrite before confirming a recovery path

The short version

git-apply is used to apply patch content to the working tree or index.

When it is a good fit

  • when you need to apply patch content to the working tree or index
  • when you want this step to be repeatable instead of ad hoc
  • when you need a clearer mental model of what Git is recording or updating

Basic example

git apply fix-login.patch

What to watch most closely

Patch-based workflows are sensitive to context drift, so confirm the base state and the recovery path first.

A safer working habit

Read the patch series first and keep abort, continue, or a backup branch ready.

Useful angles for understanding it

  • Handle mailbox patches or patch files
  • Exchange changes outside direct repository access
  • Preserve commit boundaries instead of copying edits manually

Related reading

Read it alongside git status, git log, and git show so it is easier to see how the command changes history, refs, the index, or the working tree.

What problem this command solves in a workflow

git apply applies patch content to the working tree or index without creating commits. Unlike git am, it does not parse email headers or generate commit objects — it is purely a patch application tool for "how do I incorporate these changes into my working copy?"

Typical use cases

  • Apply a diff or patch file to the working tree for review before deciding whether to commit: git apply fix.patch.
  • Use git apply --check to verify a patch applies cleanly without actually modifying any files.
  • Apply patches to the index directly with git apply --index, staging the changes without touching the working tree.

Diagram view

Patch-to-working-tree applicationapply reads patch content and writes changes to the working tree or index. It does not create commits or parse email metadata.
Input
Patch/diff fileCurrent working tree
Output
Modified working tree filesOptionally staged changes (with --index)
apply changes file content only. You still need to review, stage, and commit manually afterward.

Special cases and boundaries

  • git apply does not create commits — you must review, stage, and commit the changes yourself afterward.
  • Use --check to test whether a patch applies cleanly before actually modifying files, avoiding partial application.
  • If a patch was generated with different line endings or context lines, application may fail; --ignore-whitespace or --3way can help.
  • Unlike git am, git apply does not preserve author information or commit messages from the original patch.