我将git提交一个.sh文件,但希望当我在另一个服务器签出相同的文件时,它是可执行的。
有没有一种方法来这样做,而不手动chmod u+x文件在服务器签出文件?
我将git提交一个.sh文件,但希望当我在另一个服务器签出相同的文件时,它是可执行的。
有没有一种方法来这样做,而不手动chmod u+x文件在服务器签出文件?
根据官方文档,您可以使用update-index子命令在任何跟踪文件上设置或删除“可执行”标志。
设置标志,使用以下命令:
git update-index --chmod=+x path/to/file
要删除它,请使用:
git update-index --chmod=-x path/to/file
在引擎盖下
虽然这看起来像常规的unix文件权限系统,但实际上不是。Git为内部存储中的每个文件维护了一个特殊的“模式”:
普通文件100644 100755用于可执行文件
你可以使用ls-file子命令——stage选项来可视化它:
$ git ls-files --stage
100644 aee89ef43dc3b0ec6a7c6228f742377692b50484 0 .gitignore
100755 0ac339497485f7cc80d988561807906b2fd56172 0 my_executable_script.sh
默认情况下,当您将一个文件添加到存储库中时,Git将尝试遵循其文件系统属性并相应地设置正确的文件模式。你可以通过设置core来禁用它。fileMode选项为false:
git config core.fileMode false
故障排除
如果在某些时候Git文件模式没有设置,但文件有正确的文件系统标志,尝试删除模式,并重新设置它:
git update-index --chmod=-x path/to/file
git update-index --chmod=+x path/to/file
奖金
从Git 2.9开始,你可以在一个命令中创建一个文件并设置标志:
git add --chmod=+x path/to/file
Antwane的回答是正确的,这应该是一个评论,但评论没有足够的空间,不允许格式化。:-)我只是想补充一点,在Git中,文件权限只记录为644或755(拼写为(100644和100755;100部分表示“常规文件”):
diff --git a/path b/path
new file mode 100644
The former—644—means that the file should not be executable, and the latter means that it should be executable. How that turns into actual file modes within your file system is somewhat OS-dependent. On Unix-like systems, the bits are passed through your umask setting, which would normally be 022 to remove write permission from "group" and "other", or 002 to remove write permission only from "other". It might also be 077 if you are especially concerned about privacy and wish to remove read, write, and execute permission from both "group" and "other".
1极早期版本的Git保存了组权限,因此一些存储库中有模式为664的树条目。现代Git没有,但由于任何对象的任何部分都不能被更改,这些旧的权限位仍然保存在旧的树对象中。
只存储0644或0755的更改是在提交e44794706eeb57f2中,它在Git v0.99之前,日期为2005年4月16日。
为存储库中的所有文件设置可执行标志:
git ls-files --stage |grep 100644 | cut -f2 | xargs -d '\n' git update-index --chmod=+x
要对所有文件取消可执行标志,请执行相反的操作
git ls-files --stage | grep 100755 | cut -f2 | xargs -d '\n' git update-index --chmod=-x
... 要设置所有的.sh脚本可执行文件,也许这是你的方法:
git ls-files --stage | grep ".sh$" | cut -f2 | xargs -d '\n' git update-index --chmod=+x
在linux上,不要忘记这样做
set sudo chmod +x /path/to/file
除了在本地进行git更新之外,否则git总是会将索引带回644,这是本地机器默认设置的!
在Windows Powershell中,您可以使用
icacls .\path\to\file /grant Everyone:F