Error when trying to apply git diff, "No valid patches in input"
Asked Answered
G

6

18

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

Grim answered 13/6, 2022 at 18:36 Comment(3)
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.Pernell
UTF-16 was my issue. From powershell you can fix it with Get-Content .\my.patch | Set-Content -Encoding utf8 .\myutf8.patchFlexible
when i do this in bash, it takes my color codings that are applied to the normal diff, so the patch is corrupt, and dos2unix wont resolve it. e.g. i see a bunch of [1;33m at the line beginnings. Turns out it was my alias setup, which i used gd by muscle memory but git diff worked fine.. Thanks!Octans
M
8

I have the same problem as well when applying the patch. When I opened the patch, I got the patch with UTF-16 LE BOM encoding. Quite likely this was the problem.

So instead of using

# don't do this
git diff > myoldpatch.patch

for creating the patch of unsaved changes, using Powershell, you do this instead (thanks benf!)

git diff | Set-Content -Encoding utf8 .\goodutf8.patch

PS. tried in git 2.42

Matland answered 20/9, 2023 at 18:11 Comment(3)
An alternative is to create the patch from Windows Command Prompt (cmd.exe).Astir
This worked! Apologies for taking so long to accept itGrim
As @Jordan suggested above another option is to use git bash, which wont be creating this problem.Ida
H
36

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.

Hesper answered 10/1, 2023 at 13:20 Comment(1)
For me, CRLF was not an issue. But the format was and saving it in UTF-8 helped.Backbend
S
11

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

Shook answered 22/5, 2023 at 8:37 Comment(0)
M
8

I have the same problem as well when applying the patch. When I opened the patch, I got the patch with UTF-16 LE BOM encoding. Quite likely this was the problem.

So instead of using

# don't do this
git diff > myoldpatch.patch

for creating the patch of unsaved changes, using Powershell, you do this instead (thanks benf!)

git diff | Set-Content -Encoding utf8 .\goodutf8.patch

PS. tried in git 2.42

Matland answered 20/9, 2023 at 18:11 Comment(3)
An alternative is to create the patch from Windows Command Prompt (cmd.exe).Astir
This worked! Apologies for taking so long to accept itGrim
As @Jordan suggested above another option is to use git bash, which wont be creating this problem.Ida
G
2

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

Greenwell answered 29/9, 2022 at 15:38 Comment(1)
it would be nice if you had added how to actually do that...Repetend
F
1

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

Floris answered 9/3, 2023 at 7:9 Comment(1)
dude you just saved my day! I used difftastic and it was messing with git diff!Armil
P
1

Just run "dos2unix your_patch.patch" on your patch file and it should work after. dos2unix should be available on all distros.

Puny answered 21/5 at 8:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.