Git has been a reliable version control tool for a large number of closed and open-source projects. With Drupal being an extremely collaborative open-source content management framework, a trackable, transparent, and distributed version control system like Git is a perfect fit (hey, that rhymes!). Git replaced a long-time version control partner – CVS – in early 2011 and became every Drupal developer and contributor’s favorite tool for its security, distributed nature, agile workflow, and ofcourse, being open source!

In this article, you’ll learn more about Git patches and different ways to use them. We’ll also talk about some Git Diff commands and how to create/apply a patch with the help of Git Diff and Git Apply. We will assume that you have already cloned/obtained a copy of the project in your local repository and have pulled the latest changes, so you’re not working on an older version of the project. Take a look at some GitHub best practices here.

First published in 2020, this article was updated in 2023 and again in October 2025 to keep things current and accurate. We’d like to thank Akshay Devadiga, Anktiha Shetty and Pratik Kadambari for all their great inputs!

🎧 Listen to the audio version of this guide 

What is a patch?

If you’re a Drupal developer, you should be familiar with Git patches already. Patches are like Band-Aids. They are small pieces of code that are added on top of already existing code files to support them or fix any issues. Different types of patches include bug fixes, security vulnerability fixes, performance enhancements, styling fixes, etc. If you are a regular contributor to the Drupal project, you should know that to fix an issue in Drupal core or contributed modules, you must submit a patch to an issue in the issues queue. These patches are then examined and tested by the module maintainer and applied if found beneficial.

Struggling with patches or other Drupal tasks? Get in touch with our Drupal development company today for a hassle-free experience!

What is a Git Patch?

A Git patch contains a record of your code changes between two different versions of your code (or between two commits). It captures what lines were added, removed, or modified, making it easy to share or apply these changes elsewhere.

There are two common ways to create patches in Git:

  1. Git diff patch – This is created with the git diff command (we’ll talk about it in detail next). This captures only the line-by-line changes between your working directory or commits. For example:
git diff > myUpdates.patch
  1. Git format-patch – This is created with the git format-patch command. This generates patches for one or more commits and includes commit metadata like author, date, and commit message. These patches can be applied with git am and are often used for sending changes to mailing lists or contributing to projects like Drupal. Example:
git format-patch HEAD~1

Git Diff, Git Format-Patch, and Git Apply: What’s the difference?

If you’re trying to figure out the differences between Git patch types and how to apply changes, this table breaks down Git Diff, Git Format-Patch, and Git Apply, what they do, and how they work.

Git Diff Git format-patch Git apply
Shows differences between two sources (working dir, staging area, commits) Generates a patch from one or more commits Applies a patch to a repository
Outputs line-by-line changes; no commit metadata Includes commit metadata (author, date, message) Reads a patch file and applies changes; does not create commits
Can redirect to a file to create a patch Produces one file per commit by default Can reverse changes with -R

When should you use a Git Patch?

A Git patch is useful anytime you want to share or apply changes without directly pushing to a remote repository. Common scenarios include:

  1. Contributing to open-source projects
    Many projects, including Drupal, accept contributions as patches instead of pull requests. Using git diff or git format-patch lets you package your changes and submit them for review, making collaboration easier.
  2. Sharing changes between repositories or developers
    If you’re working in a team or across multiple repositories, patches allow you to send precise changes without merging full branches. This is especially helpful for code reviews or when access to a remote repo is limited.
  3. Applying quick fixes or experiments
    You can generate a patch from your local changes and apply it to another branch or clone for testing, debugging, or experimenting — all without committing to the main branch.
  4. Offline or isolated workflows
    In environments where network access is limited or controlled, patches let you transfer changes via email, file shares, or version-controlled backups.

What does the Git Diff command do? 

The Git Diff command shows you what’s changed between two points in your repository. That could be between branches, commits, or even individual files. It’s essentially a side-by-side comparison tool that helps you see exactly what’s been added, removed, or modified in your code.

Here are some of the most common ways developers use git diff:

$ git diff

This command will output all the modified changes that are not added to Git or staged.

git diff command

$ git diff filename

This will output the changes of that current file to its previous committed state.

git-diff-filename

$ git diff branch_name

This will output the modifications of the current branch to the mentioned branch to its previous committed state.

git-branch-name

$ git diff --staged (or --cached) path/to/file

Once the changes are added to Git or moved to staging, you will not be able to see the diff of the files. To see the staged changes, you can use diff with --staged or --cached option.

git diff staged path

$ git diff HEAD

To see the changes from both staged and unstaged changes (not untracked files) together, you can use the git diff HEAD command. If you have all changes staged for commit, then both commands i.e., --staged/–-cached and HEAD will output the same.

Staged

Git Diff Head

$ git diff commit_id1 commit_id2

To see the difference between any two commits you can use this git diff command where you need to mention the two commit ids.

git-commit
 

If you want to see the list of commits made in the Git repo, use the command $ git log. This will list out all the commits (starting from the latest commit) along with their respective commit IDs, the author (developer) and the date it was committed.

Get UI UX services package now

How to create a Git patch?

To create a Git patch, we can use any of the Git diff commands to get the changes. We then need to save the changes to a file, which can be used as follows.

$ git diff > my_custom_patch_file.patch

Custom patch

Git Diff

Commit

How to apply a patch in Git? 

Drupal developers often use Git patches to apply updates, fix bugs, or share code changes between projects and team members. A patch file contains the exact changes that need to be applied, making collaboration simple and precise. To apply a patch to your current branch, you can use the following command:

$ git apply patch_file.patch 

Patch file

Try the above command with the -v or --verbose option. It will cause additional information about the current patch being applied to be reported (as shown below).

Current patch

Useful flags for Git Apply

  • --check – Verifies whether the patch can be applied cleanly without making changes.
  • --reverse or -R – Reverts a previously applied patch.
  • --verbose – Shows detailed output while applying the patch.
  • --stat – Displays a summary of files changed by the patch.
  • --reject – Creates .rej files for failed hunks instead of aborting the entire patch.

Advanced Git Apply options for patches

When applying patches in Git, there are several options that make the process safer, more precise, and adaptable to different situations. Here’s a detailed guide for Drupal developers and anyone working with Git patches.

1. Handling trailing white spaces

Sometimes a patch may include accidental trailing whitespaces, which can cause linting errors or warnings. By default, applying such a patch will retain these whitespaces:

git apply patch_file.patch

whitespace

To automatically fix trailing whitespaces while applying the patch, use:

git apply --whitespace=fix patch_file.patch

This ensures the patch is applied cleanly without introducing unnecessary whitespace issues.

2. Checking the patch before applying

Before making changes to your branch, it’s good practice to verify whether the patch can be applied cleanly. The --check option does exactly that:

git apply --check patch_file.patch

check option

Check patch

If the patch is clean, Git will show no warnings or errors. This helps prevent broken or partial applications.

3. Viewing file statistics before applying the patch

Sometimes you just want to see what files will be affected by a patch without actually applying it. The --stat option lists all files that will change:

git apply --stat patch_file.patch

Stat

To apply the patch and see the file statistics at the same time, combine it with --apply:

git apply --stat --apply patch_file.patch

4. Applying only the removed changes

If you want to apply only the deletions (lines being removed) and skip additions, use --no-add:

For example:

No add option

git apply --no-add patch_file.patch

No add patch

This is useful when you want to selectively apply parts of a patch without introducing new lines.

5. Including or excluding specific files

You can control which files a patch is applied to:

  • Exclude files:
git apply --exclude=file_name.ext patch_file.patch

All changes except the excluded file will be applied.

  • Include files:
git apply --include=file_name.ext patch_file.patch

Only changes for the included file will be applied.

When to use Git Am vs Git Apply

While both git apply and git am work with patch files, they serve different purposes:

  • git apply applies changes directly to your working directory without creating commits. Use it when you want to quickly apply patches, test changes, or handle partial updates.
  • git am applies patches as commits, preserving the original author, commit message, and metadata. This is ideal when contributing to projects, submitting patches to mailing lists, or maintaining a clear commit history.

Git Patch best practices and tips

Mastering these git apply options helps you apply patches more safely, avoid common errors, and streamline collaboration, especially for Drupal developers managing modules, themes, or core updates.

  • Always run git apply --check before applying patches to avoid breaking your branch.
  • Use --verbose to see detailed output while applying patches.
  • If a patch fails partially, try --reject to generate .rej files for failed hunks and manually resolve conflicts.
  • Keep patch files organized with descriptive names like bugfix-login-issue.patch.
  • For Drupal contributions, follow project-specific patch guidelines; sometimes, git format-patch with commit metadata is preferred.

Final thoughts

Sometimes, you might want to suggest a change or fix a bug but don’t have write access to a project. In those cases, creating a Git patch is the simplest and most effective solution. Patches are easy to test, review, and share, making collaboration smooth and precise.

This guide is designed to help Drupal developers get comfortable with Git Diff and Git Apply, so you can efficiently create and apply patches whenever needed. If you’re looking for expert help with your next Drupal project or want guidance on patch workflows, contact us. We’re happy to help!

Contact us

LET'S DISCUSS YOUR IDEAS. 
WE'D LOVE TO HEAR FROM YOU.

CONTACT US SUBMIT RFP