4

I'm creating a powershell script with the goal of going through several git branches, saving a patch of the working changes, switching to the trunk branch to pull remote commits, then switching back to the working branch and reapplying the working changes.

Here is the method I am testing specifically:

git diff > test.patch
git restore .
*do main branch operation*
git apply test.patch

However when I try to apply the patch that I have just created I get an error:

error: No valid patches in input (allow with "--allow-empty")

Does anyone see what I'm doing wrong here?

Git version: 2.35.2.windows.1

Powershell version: 5.1.19041.1320

Ridiculon
  • 323
  • 4
  • 12
  • 6
    Most likely PowerShell has corrupted the patch by turning it into UTF-16-LE. You can, however, get similar effects by forcing the `git diff` command to use color. This is why robust scripts avoid `git diff` and use instead the various plumbing commands. – torek Jun 13 '22 at 21:59
  • 3
    UTF-16 was my issue. From powershell you can fix it with `Get-Content .\my.patch | Set-Content -Encoding utf8 .\myutf8.patch` – benf Dec 14 '22 at 17:37

4 Answers4

15

Likely windows changed the line terminator to CRLF. Get it back to LF.

For anyone else struggling - I tried this, but I also had to re-save the patch file in UTF-8 format for git to recognise it.

penguinflip
  • 1,057
  • 1
  • 9
  • 19
1

I also had the same problem, but I got it solved, use Notepad to open the patch file, select UTF-8 in the code selection, then save it, and then use git apply patch to enter the patch

0

Likely windows changed the line terminator to CRLF. Get it back to LF.

user1593165
  • 503
  • 3
  • 6
0

In my case the reason was the external diff helper set in git config, so specifying the --no-ext-diff flag helped

Arsen
  • 118
  • 1
  • 6