我创建的所有 GIT 补丁都抛出了致命错误:无法识别的输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37347350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
All GIT patches I create throw fatal: unrecognized input
提问by lacostenycoder
I'm running Mac OSX 10.9.4 (Mavericks) and have git version 2.8.2. I've tested this with a completely new repo. Here's example.
我正在运行 Mac OSX 10.9.4 (Mavericks) 并且有 git 版本 2.8.2。我已经用一个全新的 repo 对此进行了测试。这是示例。
mkdir gitest
cd gitest
git init
echo "monkeyface" > monkey.txt
git commit -m "first commit"
echo "monkeyface farted" > monkeyfart.txt
git add .
git diff HEAD > new.patch
rm monkeyfart.txt
git reset --hard HEAD
git apply new.patch --check
>fatal: unrecognized input
Any ideas what is causing this? Could it be anything in my .gitconfig file?
任何想法是什么导致了这种情况?我的 .gitconfig 文件中可能有任何内容吗?
[user]
name = myusername
email = [email protected]
[color]
ui = always
[alias]
st = status -sb -uall
lg = log --decorate --pretty=oneline --abbrev-commit --graph
undocommit = reset --soft HEAD^
undopush = push -f origin HEAD^:master
[core]
editor = vim
excludesfile = ~/.gitignore_global
pager = less -r
[commit]
template = ~/.gitmessage.txt
[filter "media"]
clean = git-media-clean %f
smudge = git-media-smudge %f
UPDATE:
更新:
While the answer linked below offers some idea on what the problem might have been, my issue was specifically hidden in my configuration since no color argument was being passed into the command. This answer is relevant but my question and answer might be helpful to others who may experience a similar issue.
虽然下面链接的答案提供了一些关于问题可能是什么的想法,但我的问题特别隐藏在我的配置中,因为没有颜色参数被传递到命令中。这个答案是相关的,但我的问题和答案可能对可能遇到类似问题的其他人有所帮助。
回答by Nguy?n ??t
There is format problem in patch file. To fixthe path file:
补丁文件格式有问题。修复路径文件:
Open your patch file in notepad++ then enter these two menus:
Encoding/Convert to UTF-8 Edit/EOL conversion/Unix (LF)
Run:
git apply --reject --whitespace=fix your_patch.patch
在记事本++中打开你的补丁文件,然后进入这两个菜单:
Encoding/Convert to UTF-8 Edit/EOL conversion/Unix (LF)
跑:
git apply --reject --whitespace=fix your_patch.patch
回答by lacostenycoder
Updated
更新
You might have a file which was not encoded to UTF-8. To fix that on *nix systems (MacOS, Linux etc.)
您可能有一个未编码为 UTF-8 的文件。在 *nix 系统(MacOS、Linux 等)上修复该问题
iconv -f ascii -t utf-8 fix.patch -o fix_utf8.patch
For windows you can try:
对于 Windows,您可以尝试:
Get-Content .\fix.patch | Set-Content -Encoding utf8 fix_utf8.patch
If your file may already have color codes in it you can try:
如果您的文件中可能已经包含颜色代码,您可以尝试:
git apply --reject --whitespace myfile.patch
Passing in color param seems to fix the problem.
传递颜色参数似乎解决了这个问题。
git diff HEAD --color=never > fix.patch
And now check returns no error message.
现在检查没有返回错误信息。
git apply fix.patch --check
Changing my .gitconfig file from
更改我的 .gitconfig 文件
[color]
ui = always
change to always
更改为始终
[color]
ui = auto
Fixed my problem so I do not have to pass color option when diffing to patch file.
修复了我的问题,因此在与补丁文件不同时我不必传递颜色选项。
UPDATE: Based on saurabheights answer, you don't even need to brew link gnu-sed
, you can do this with pearl. This will removed color characters from the bad patch file as well. There are probably many ways to do this.
更新:根据 saurabheights 的回答,你甚至不需要brew link gnu-sed
,你可以用珍珠来做到这一点。这也将从坏补丁文件中删除颜色字符。可能有很多方法可以做到这一点。
perl -pe 's/\x1b.*?[mGKH]//g' bad.patch > good.patch
回答by Chirag Bhatia - chirag64
We tried debugging this for a few hours. What finally worked was this:
我们尝试调试了几个小时。最终奏效的是:
- Opened patch file with an editor like
VS Code
- Changed encoding to
UTF-8
- Changed line endings from
CRLF
toLF
- Saved the new file
git apply myPatch.patch
worked
- 使用类似的编辑器打开补丁文件
VS Code
- 将编码更改为
UTF-8
- 将行尾从 更改
CRLF
为LF
- 保存了新文件
git apply myPatch.patch
工作过
回答by saurabheights
For those who already have a patch with color codes, try this:-
对于那些已经有颜色代码补丁的人,试试这个:-
On Ubuntu:-
在Ubuntu 上:-
cat incorrect.patch | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" > correct.patch
On Mac, install gsed using:-
在Mac 上,使用以下命令安装 gsed:-
brew link gnu-sed
and to generate correct patch file:-
并生成正确的补丁文件:-
cat incorrect.patch | gsed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" > correct.patch
回答by Hadi Fooladi Talari
On Windows, For Visual Studio:
在 Windows 上,对于 Visual Studio:
- Open the file
- From Menu: File -> Save filenameAs...
- Select 'Save with Encoding...'
- 打开文件
- 从菜单:文件 -> 将文件名另存为...
- 选择“使用编码保存...”
- Select the following:
- Encoding: UTF-8 with/without signature
- Line endings: Unix (LF)
- 选择以下内容:
- 编码:带/不带签名的UTF-8
- 行尾:Unix (LF)
- Save the file
- Run
git apply MyPatch.patch
again
- 保存文件
git apply MyPatch.patch
再次运行