Applying and Creating Patches with Git

Video loading...

  • 0:00
    [Chapter 17: Introduction to Git]
  • 0:02
    [Applying and Creating Patches with Git with Blake Hall]
  • 0:05
    [Blake Hall]: Since Drupal itself uses Git,
  • 0:07
    we can take a look at how to apply and create patches
  • 0:09
    on with a few simple commands.
  • 0:12
    We'll take a look at git apply
  • 0:14
    for applying and testing patches in the issue queue,
  • 0:17
    and git diff and git format-patch
  • 0:19
    to create our own patches to contribute back to the community.
  • 0:23
    Let's take a look at using Git
  • 0:25
    to apply and create patches.
  • 0:28
    We'll use for these examples.
  • 0:30
    So let's grab a copy so we can get to work.
  • 0:33
    From the homepage we'll click "Getting Started with Drupal"
  • 0:35
    and download Drupal 7.20.
  • 0:39
    From the main project page,
  • 0:41
    you can see there's a Version Control tab.
  • 0:43
    If we go ahead and click here,
  • 0:45
    we'll get some instructions on how we can clone the repository.
  • 0:49
    In this case, let's work from version 8.
  • 0:51
    We can copy this command and paste it into terminal
  • 0:54
    to download our copy of Drupal.
  • 0:56
    Let's go ahead and do that.
  • 0:58
    Now that we have our copy of Drupal downloaded,
  • 1:00
    let's find an issue that we want to work on.
  • 1:02
    We'll click on Advanced Search,
  • 1:04
    a status of "needs review"
  • 1:06
    since that's likely to have a patch,
  • 1:09
    and for Component let's choose "user interface text,"
  • 1:13
    and a "novice" issue tag
  • 1:15
    so the patch will be straightforward and easy to follow.
  • 1:20
    "Improve the maintenance page error message."
  • 1:23
    That seems like a good issue to take a look at.
  • 1:29
    Scrolling through the issue here we'll notice
  • 1:31
    people uploading patch files as work on this issue progresses.
  • 1:36
    The most recent patch will be down at the bottom of the file.
  • 1:40
    The fact that this patch is green
  • 1:42
    indicates that it has passed tests from Drupal's automated testing infrastructure,
  • 1:46
    which is why the issue is marked "needs review."
  • 1:50
    Let's go ahead and apply that patch to our Drupal install.
  • 1:53
    In order to do that, we can click on the attachment title here
  • 1:56
    and actually view the patch file itself.
  • 1:59
    Since we've taken a look at git diff already,
  • 2:01
    this should look very familiar.
  • 2:03
    It's basically the output of the git diff command.
  • 2:06
    In order to actually apply this patch file,
  • 2:08
    we'll need to download it to our local copy of Drupal.
  • 2:10
    We can do that by copying the URL,
  • 2:12
    switching to our terminal,
  • 2:14
    and using the wget command
  • 2:16
    to pull in the patch.
  • 2:18
    Now that the patch is downloaded, we need to apply it.
  • 2:21
    We can use the command "git apply"
  • 2:23
    and then the name of the patch to do this.
  • 2:27
    That's all there is to it.
  • 2:28
    If we run git status, we'll see that files have been modified
  • 2:32
    with the changes from the patch.
  • 2:34
    If we compare that to the diff file we saw on the website,
  • 2:37
    we can see that core/includes/
  • 2:41
    and core/lib/Drupal/Core/ExceptionController
  • 2:44
    are the 2 files that should be modified by this patch,
  • 2:47
    which matches the output of our git status.
  • 2:50
    We can now go through and take a look at the Drupal site
  • 2:53
    and make sure that the patch works
  • 2:55
    as expected.
  • 2:57
    You can test and apply any patch from
  • 2:59
    in a similar fashion.
  • 3:01
    The patch may not always apply so cleanly,
  • 3:03
    and if it doesn't, leaving a comment on the issue queue
  • 3:06
    is helpful to let people know that a new patch needs to be created.
  • 3:09
    Now that we've seen how to apply patches,
  • 3:11
    let's try our hand at creating one.
  • 3:13
    Here I found a Drupal issue that I'm interested in working on.
  • 3:16
    Since this issue requires porting to Drupal 7,
  • 3:19
    we'll need to check out the 7.x branch in our local.
  • 3:22
    We'll do that with a tracking branch, so I'll pass the -t flag
  • 3:24
    and then the name of the remote branch we wish to track.
  • 3:29
    We've now checked out the 7 branch
  • 3:32
    from the repository
  • 3:34
    and switched to it on our local.
  • 3:36
    We can now try our hand at making the changes to fix the issue.
  • 3:40
    Okay, we've gone ahead and done the work necessary to fix the issue.
  • 3:43
    If we take a look at the output of git status,
  • 3:45
    we can see that we've modified 1 file,
  • 3:47
    and the output of git diff will show us
  • 3:49
    the changes that we've made.
  • 3:51
    In order to generate a patch file that we can upload to,
  • 3:54
    all we need to do is redirect the output of this git diff command
  • 3:57
    and save it to a patch file.
  • 3:59
    It's generally a good idea to name your patch file
  • 4:02
    with the issue queue number and a descriptive name
  • 4:04
    of what it is that you're fixing.
  • 4:06
    Let's go ahead and copy that issue queue number so we can create our patch.
  • 4:09
    From the command line, we'll run that git diff command again,
  • 4:15
    and we'll redirect the output
  • 4:17
    to a file with the issue queue number
  • 4:19
    and say "cleaning-up-documentation.patch."
  • 4:28
    After that command runs,
  • 4:30
    if we run a directory listing we'll be able to see our new patch file created.
  • 4:34
    Taking a look at that patch file
  • 4:36
    with the "more" command,
  • 4:38
    we'll see that the output is identical
  • 4:40
    to what we generated with git diff.
  • 4:42
    This file is now ready to be uploaded to the issue queue
  • 4:46
    for testing by other developers.
  • 4:48
    Another more useful way of generating patch files
  • 4:51
    is using the git format-patch command.
  • 4:54
    The git format-patch requires that we've actually committed
  • 4:56
    this code on our local repository.
  • 5:00
    So let's go ahead and do that.
  • 5:02
    In our commit message we'll be sure to indicate the issue number
  • 5:05
    for the patch that this fixes.
  • 5:11
    Now that we've committed our code to our local repository,
  • 5:14
    we're ready to use git format-patch.
  • 5:17
    Git format-patch takes a couple of arguments.
  • 5:20
    One being the name of the remote branch
  • 5:24
    in which you wish to create patch files.
  • 5:27
    Then we'll pass the -stdout flag
  • 5:30
    so we can see the output of git format-patch in our terminal.
  • 5:32
    After we run the git format-patch command,
  • 5:34
    we'll see the output looks mostly the same as git diff
  • 5:37
    with a few key differences.
  • 5:39
    The main difference are these lines at the top.
  • 5:42
    Git format-patch was designed to be used
  • 5:45
    to create patch files that could be sent
  • 5:48
    automatically via email.
  • 5:50
    One of the main advantages to using git format-patch
  • 5:52
    is it uses proper author attribution,
  • 5:55
    which makes tracking people's contributions
  • 5:57
    to much easier.
  • 5:59
    You can use either git diff or git format-patch
  • 6:01
    to create patch files to upload to
  • 6:03
    If you'd like to read more details
  • 6:05
    about this process,
  • 6:07
    and the differences between the two,
  • 6:09
    there's an article on
  • 6:11
    that outlines this process in more detail.
  • 6:14
    [Drupalize.Me: Learn Drupal]

Introduction to Git

Loading ...

Applying and Creating Patches with Git


This lesson takes a look at applying a patch from to your local copy of a module or Drupal core using Git. Afterwards we'll look at how you can create your own patches, using git diff and git format-patch, in order to contribute code back to Drupal or any of the module's on You can see a full Git workflow using GitHub in the lesson Git Workflow: Putting It All Together.

Average review

No votes yet