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
Leave a Reply