0

What mistake am I making in the steps I'm following?

I've edited files in repo Alpha on my local box. I then realized I wanted those changes in a different repo Bravo that is also on my local box. I tried this:

c:/repos/alpha/>git diff --cached > mypatch.txt

I then copy the patch file to the other repo location and type this:

c:/repos/bravo/>git apply mypatch.txt

If the shell I used for the diff and apply was powershell or "Git CMD", then the second command makes the error:

fatal: unrecognized input

If instead I use the "Git Bash" shell to execute the two commands, then I get a different error:

5109e.patch:19: trailing whitespace.
    IL.DataUsageGB,
warning: 1 line adds whitespace errors.

I then try to apply the changes more carefully with the following command:

$ git apply --reject --whitespace=fix mypatch.txt

From this I get a dump of numerous errors. Example:

error: while searching for:
);
GO
-- Anchor table ------------------------------------------------------------
-------------------------------------------
-- IL_InvoiceLine table (with 33 attributes)
----------------------------------------------------------------------------
-------------------------------------------
IF Object_ID('dbo.IL_InvoiceLine', 'U') IS NULL
CREATE TABLE [dbo].[IL_InvoiceLine] (

error: patch failed: scripts/bi/sql/Unified_ODS_Schema.sql:302

The branch in repo Alpha and the corresponding branch in repo Bravo both come from the same origin and both have a git status that report "up to date" with the upstream. In other words, the branches are identical except for the staged changes that exist on Alpha. I am expressly avoiding a push/pull with the origin.

Suggestions?

Brent Arias
  • 29,277
  • 40
  • 133
  • 234

1 Answers1

0

TL;DR

There's nothing wrong, and you can completely ignore the warning. You don't need --reject or --whitespace=fix, but if you do want to use the latter, use it without the former.

Longer

If the shell I used for the diff and apply was powershell ...

This winds up writing the output as Unicode (through some mechanism I cannot describe properly since I don't "do" Windows). You'd have to filter that back to UTF-8 or ASCII to get it to apply.

If instead I use the "Git Bash" shell to execute the two commands, then I get a different error:

5109e.patch:19: trailing whitespace.
    IL.DataUsageGB,
warning: 1 line adds whitespace errors.

That's not really an error, that's a warning. It means that your original patch adds a blank before an end-of-line. By default, git apply calls this an "error" but it really means "warning". It's meant to alert you to the fact that there's an invisible character on the line(s) in question, which you may not have intended. (Or maybe you did! For instance, in some Markdown formats, ending a line with two blanks inserts a paragraph break. See aslo Git ignore trailing whitespace in markdown files only.)

What constitutes a "whitespace error" (which really should be "whitespace annoyance" or "whitespace warning" or "whitespace glitch" everywhere, rather than "error") is configurable. By default git diff will highlight such whitespace glitches. While I cannot quite show it here, imagine the - line is in red and the + line is in green and that <space> represents a trailing blank:

- blah blah
+ blah foo blah<space>

This space would be highlighted in red, to make it stand out as a "whitespace error" (which I would call a whitespace glitch or annoyance or warning, but as long as we are using Git we should understand what the phrase "whitespace error" means here).

With --whitespace=fix, git apply will find the things it considers to be whitespace errors and determine whether it can fix them automatically by:

  • stripping trailing whitespace
  • removing some space-before-tab spaces
  • fussing with CRLF vs LF-only

If it can fix them, it will. This includes applying the patch even if the context does not quite match up but can be made to do so by this kind of fussing, so it's more than just "removing trailing whitespace in the added lines".

torek
  • 448,244
  • 59
  • 642
  • 775