gpt4 book ai didi

git - 如何在 Git 中为文件添加 chmod 权限?

转载 作者:IT王子 更新时间:2023-10-29 01:21:44 24 4
gpt4 key购买 nike

我会 git 提交一个 .sh 文件,但是当我在另一台服务器上 check out 同一个文件时,我希望它是可执行的。

有没有办法不用手动 chmod u+x check out 文件的服务器中的文件?

最佳答案

根据 official documentation ,您可以使用 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 来禁用它假选项:

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

关于git - 如何在 Git 中为文件添加 chmod 权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40978921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com