Reputation: 525
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
Upvotes: 21
Views: 23281
Reputation: 151
Just run "dos2unix your_patch.patch" on your patch file and it should work after. dos2unix should be available on all distros.
Upvotes: 1
Reputation: 401
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
Upvotes: 17
Reputation: 121
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
Upvotes: 12
Reputation: 168
In my case the reason was the external diff helper set in git config, so specifying the --no-ext-diff
flag helped
Upvotes: 3
Reputation: 1377
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.
Upvotes: 39
Reputation: 615
Likely windows changed the line terminator to CRLF. Get it back to LF.
Upvotes: 2