• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Dev Tips / Your edited hunk does not apply

Your edited hunk does not apply

Last updated on March 24, 2021 by Sal Ferrarello

When using git add -p and faced with a hunk that can not be split sufficiently for your purposes, you can use the e option (manually edit the current hunk).

Unfortunately, after manually editing the current hunk it is common to get the message:

error: patch failed: mycode.js:1
error: mycode.js: patch does not apply
Your edited hunk does not apply. Edit again (saying "no" discards!) [y/n]?

The Secrets to Manually Editing a Hunk

The First Column is Not Part of Your Code

The first column of each line is either:

  • a plus sign (+), this line is being added
  • a minus sign (-), this line is being removed
  • a blank space (), this line is for reference

Typically when staging only some of the lines in your hunk you want to either:

  • delete a line that starts with a plus (+) i.e. don’t add the line
  • change a minus sign (-) to a space () i.e. don’t delete the line

When removing the minus (-) sign make certain you replace it with a space.

Adjusting the Range Values

Each hunk is a unified diff, which includes a range value at the beginning of the hunk like the following, which represents where the change is being made and how many lines are being changed.

@@ -1,8 +1,18 @@

When modifying the hunk of code it is very common to get the failure message,

Your edited hunk does not apply.

if you do not modify your range values.

Generally, modifying the final number in the range (e.g. 18 in the above example) will allow your hunk to apply.

  • when you delete a line starting with a plus (+), reduce the final number in the range by one
  • when you change line starting minus (-) into a space (), increase the final number in the range by one
Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit
Warning! This is a draft, not a finalized post. See full draft disclosure.

Filed Under: Dev Tips, Draft, Solution Tagged With: Git

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in