Blogs

EMAIL: info@example.com

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

How to create and apply a patch with Git Diff and Git Apply commands for your Drupal website
Akshay Devadiga
18 Aug, 2020

Git has been a reliable version control tool for a large number of closed and opensource projects. With Drupal being an extremely collaborative opensource 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 contributor’s favorite tool for its security, distributed nature, agile workflow and of course, being opensource!

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 to 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 need to 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.
git apply patch
There are different ways to apply 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 used 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 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 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
 

git-diff

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 which can be used by other developers according to their need. To apply a git patch to the current branch use the following command.
 

•    $ git apply patch_file.patch 

git apply patch file
In the above example I have created a patch file called my_custom_patch_file.patch and I am applying that patch to my current branch. After applying a patch, you can see the modification in the status.

Shefali ShettyApr 05, 2017