2

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?

mark
  • 59,016
  • 79
  • 296
  • 580
  • 4
    Not really, no - `git diff` always generates a unified diff. You could set the number of context lines to zero with `-U0`; that's as close as you'll get to "yes". To get a non-unified context diff, use the non-Git diff utility included with your OS. – torek Dec 08 '21 at 05:44

2 Answers2

0

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

By typing 's' for 'split' ("split the current hunk into smaller hunks"), 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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • So this is an interactive procedure. Any way to make it non interactive? Besides piping responses, which one should know beforehand. – mark Dec 08 '21 at 15:02
  • @mark Not that I know of: at some point, the diff tool needs a manual input on how small to go. – VonC Dec 08 '21 at 15:16
  • to me `s` is for `split`, but `smaller` also works ;-), as per comment in the help `s - split the current hunk into smaller hunks` – Julien Oct 31 '22 at 10:11
  • @Julien You are right! I have edited the answer accordingly. – VonC Oct 31 '22 at 10:17
0

git diff -U0 seems to work for me (-U1 doesn't).

mg979
  • 43
  • 5