Skip to content
Menu
Open World News Open World News
  • Privacy Policy
Open World News Open World News
Specbee: How to create and apply a patch with Git Diff and Git Apply commands for your Drupal website

Specbee: How to create and apply a patch with Git Diff and Git Apply commands for your Drupal website

Posted on January 24, 2023 by Michael G

Author:
Source

How to create and apply a patch with Git Diff and Git Apply commands for your Drupal website
Akshay Devadiga
24 Jan, 2023





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. 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 of course, being open source!

If you’re a Drupal developer, you should be familiar with 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 it 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.

This is a revised version of the article. We’d like to thank Anktiha Shetty and Pratik Kadambari for all their great inputs! 

Specbee: How to create and apply a patch with Git Diff and Git Apply commands for your Drupal website

There are different ways to apply a Git patch. Let’s learn more about various 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.

What does the Git Diff command do? 

Git diff is a command to output the changes between two sources inside the Git repository. The data sources can be two different branches, commits, files, etc.

The common use cases of git diff commands are listed below.

•    $ git diff 

This command will output all the modified changes which 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 on.

Creating a Git patch with git diff

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 below.
 

•    $ git diff > my_custom_patch_file.patch

 

Custom patch

 

Git Diff

 

Commit

Apply the Git Patch 

Drupal developers will want to apply Git patches frequently to update changes or to fix bugs. Developers will create a patch file that can be used by other developers according to their needs. To apply a git patch to the current branch 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

Other useful Git patch apply conditions:

1. Working with trailing white spaces

Use –whitespace option to handle such situations.

Here the patch is created accidentally with trailing whitespaces (as shown below).

whitespace

 

If the patch is applied as is: $git apply patch_file.patch
The patch will be applied (with whitespace warnings) and will still retain the whitespaces which can cause linting errors.

To fix trailing whitespaces while applying the patch, use the option –whitespace=fix, the patch will be applied, and trailing whitespaces will be removed.

Usage: $git apply –whitespace=fix patch_file.patch

2. Checking the patch before applying

To check before applying the patch, use option –check
The command will show no warnings and errors if the patch is applied cleanly.

check option

 

Check patch

 

3. To see file statistics before applying the patch

Use –stat option with the git apply command. It will list all files which will be changed upon applying the patch.

Stat

 

The patch will only show information, and will not be applied. 
To apply patch along with “stat” options add option –apply in command.

Usage: $git apply –stat –apply patch_file.patch

4. To apply only changes that are getting removed

Use Option –no-add with git apply.

For example:

No add option

 

Here only changes in red will be applied and green will be excluded.

Usage: $git apply –no-add patch_file.patch

 

No add patch

 

5. Exclude and include the patch to be applied to selected files

Use –exclude option to exclude files from applying patch change.
    
$git apply –exclude=file_name.ext patch_file.patch 
    
Changes on other files, excluding mentioned file, will be reflected.

Vice versa: 

$git apply –include=file_name.ext patch_file.patch 

Changes only on the mentioned file will be reflected.

There might be a situation when the developer does not have write access to the project but still wants to suggest a change or fix a bug. The best way to go around that is to create a patch file. And because patches are additions to the code, testing and reviewing them is easy. This brief guide aims at helping Drupal developers get more familiar with git diff and git apply commands to be able to efficiently create and apply Git patches as needed. Contact us to know more about how we can help you with your next Drupal website.

Drupal Planet
Drupal Tutorial
Drupal Development
Drupal

 

Recent Blogs

Image
apply a patch with Git Diff teaser

How to create and apply a patch with Git Diff and Git Apply commands for your Drupal website

Image
Drupal 9 on windows teaser

Installing Drupal 9 on Windows Subsystem for Linux from Scratch

Image
Drupal with Bitly teaser

How to Integrate Drupal 9 with Bitly for URL Shortening

Want to extract the maximum out of Drupal?
TALK TO US

Featured Success Stories


Physician Insurance

Upgrading and consolidating multiple web properties to offer a coherent digital experience for Physicians Insurance


IEEE Itsoc thumbnail

Upgrading the web presence of IEEE Information Theory Society, the most trusted voice for advanced technology


Great Southern Homes

Great Southern Homes, one of the fastest growing home builders in the United States, sees greater results with Drupal 9

View all Case Studies

Read more

Related Posts:

  • Specbee: The Ultimate Guide to Jumpstart your Drupal Contribution Journey
    Specbee: The Ultimate Guide to Jumpstart your Drupal…
  • Specbee: Mastering Drupal 9 Layout Builder: A Comprehensive Guide to Effortlessly Customize Your Website's Design
    Specbee: Mastering Drupal 9 Layout Builder: A…
  • Specbee: Migrate to Drupal 9 (or 10) Without Losing Your Hard-Earned SEO Ranking
    Specbee: Migrate to Drupal 9 (or 10) Without Losing…
  • Specbee: Getting Started with Layout Builder in Drupal 9 - A Complete Guide
    Specbee: Getting Started with Layout Builder in…
  • Specbee: Get the Most Out of Apache Solr: A Technical Exploration of Search Indexing
    Specbee: Get the Most Out of Apache Solr: A…
  • Specbee: What You Need To Know About Continuous Integration and Testing in Drupal
    Specbee: What You Need To Know About Continuous…

Recent Posts

  • [TUT] LoRa & LoRaWAN – MikroTik wAP LR8 kit mit The Things Network verbinden [4K | DE]
  • Mercado aguarda Powell e olha Trump, dados e Haddad | MINUTO TOURO DE OURO – 11/02/25
  • Dan Levy Gets Candid About Learning How To Act Differently After Schitt’s Creek: ‘It’s Physically…
  • Building a Rock Shelter & Overnight Stay in Heavy Snow 🏕️⛰️
  • Les milliardaires Elon Musk et Xavier Niel s’insultent copieusement

Categories

  • Android
  • Linux
  • News
  • Open Source
©2025 Open World News | Powered by Superb Themes
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT