remote: error: cannot run hooks/post-receive: No such file or directory

git push 时 钩子 post-receive 报错 error: cannot run hooks/post-receive: No such file or directory

报错信息

Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 334 bytes | 334.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: error: cannot run hooks/post-receive: No such file or directory
To 192.168.18.22:/work/test
93f10b9..30cc7c7 master -> master

问题分析

在 .git/hooks 下 post-receive 文件,却仍然报错 No such file or directory
这种情况 是 windows 下创建的文件或编辑的文件,在liunx上执行导致的,
因为各个操作系统的文件对于换行都是不一样的,对于DOS以及Windows操作系统是以CRLF标记换行,即一个回车+一个换行,而Linux或者Unix上面是以LF为标记的,即只有一个换行,因此两者的差异决定了两个系统的文件是不能随便移植的

解决方法

安装 windows文件转linux文件的工具 dos2unix, 然后转换文件

yum install dos2unix
dos2unix post-receive

结果

Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 301 bytes | 301.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To 192.168.18.22:/work/test
bf30932..328dab7 master -> master