Home > Software engineering >  How can I instruct git diff NOT to batch together hunks that are near by?
How can I instruct git diff NOT to batch together hunks that are near by?

Time:12-08

Essentially, I would like git diff to return the same hunks as returned when I chose to split the hunks in an interactive rebase.

The command I am using is:

git diff -U3 -r -M

And it can produce something like this:

@@ -70,10  70,10 @@
   "GetAllDeptJobs2": "None",
   "GetAllDeptJobsSlim": "None",
   "GetAllDirectDeposits": "Payroll (*)",
-  "GetAllEmployeeBenPlanElectedOptions": "None",
   "GetAllEmployeeBenPlanElectedOptions": "Benefits (*)",
   "GetAllEmployeeConfidentialIdentifications": "HR (*)",
-  "GetAllEmployeeDeductionBenElectedRates": "None",
-  "GetAllEmployeeEarningBenElectedRates": "None",
   "GetAllEmployeeDeductionBenElectedRates": "Benefits (*)",
   "GetAllEmployeeEarningBenElectedRates": "Benefits (*)",
   "GetAllEmployeeElectedBenefitBeneficiary": "None",
   "GetAllEmployeeElectedBenefitBeneficiaryAndDependentData": "None",
   "GetAllEmployeeElectedBenefitBeneficiarySL": "None",

The relevant portion of the respective file (after the change is applied) is:

  "GetAllDeptJobs2": "None",
  "GetAllDeptJobsSlim": "None",
  "GetAllDirectDeposits": "Payroll (*)",
  "GetAllEmployeeBenPlanElectedOptions": "Benefits (*)",
  "GetAllEmployeeConfidentialIdentifications": "HR (*)",
  "GetAllEmployeeDeductionBenElectedRates": "Benefits (*)",
  "GetAllEmployeeEarningBenElectedRates": "Benefits (*)",
  "GetAllEmployeeElectedBenefitBeneficiary": "None",
  "GetAllEmployeeElectedBenefitBeneficiaryAndDependentData": "None",
  "GetAllEmployeeElectedBenefitBeneficiarySL": "None",

I would like to get a diff where these two hunks are split. I realize the trailing context of the first hunk overlaps with the delta of the second hunk, but so what? I would like to get the following two hunks instead:

@@ -70,7  70,7 @@
   "GetAllDeptJobs2": "None",
   "GetAllDeptJobsSlim": "None",
   "GetAllDirectDeposits": "Payroll (*)",
-  "GetAllEmployeeBenPlanElectedOptions": "None",
   "GetAllEmployeeBenPlanElectedOptions": "Benefits (*)",
   "GetAllEmployeeConfidentialIdentifications": "HR (*)",
   "GetAllEmployeeDeductionBenElectedRates": "None",
   "GetAllEmployeeEarningBenElectedRates": "None",

And

@@ -72,10  72,10 @@
   "GetAllDirectDeposits": "Payroll (*)",
   "GetAllEmployeeBenPlanElectedOptions": "None",
   "GetAllEmployeeConfidentialIdentifications": "HR (*)",
-  "GetAllEmployeeDeductionBenElectedRates": "None",
-  "GetAllEmployeeEarningBenElectedRates": "None",
   "GetAllEmployeeDeductionBenElectedRates": "Benefits (*)",
   "GetAllEmployeeEarningBenElectedRates": "Benefits (*)",
   "GetAllEmployeeElectedBenefitBeneficiary": "None",
   "GetAllEmployeeElectedBenefitBeneficiaryAndDependentData": "None",
   "GetAllEmployeeElectedBenefitBeneficiarySL": "None",

I understand why git diff batched the two hunks, but is it still possible to get them separately and be able to apply the patch successfully?

CodePudding user response:

While this is not directly supported by git diff (may U1 would work?), it is with git add -p (--patch)

By typing 's' for 'smaller', you will get two hunks instead of one, and will be able to apply your first set of changes, without applying the second.

From there, you can generate a patch from your staged changes.

git diff --cached > mypatch.patch

It will include only the first hunk.

  • Related