【问题标题】:Updating file permissions with git-bash on Windows 7在 Windows 7 上使用 git-bash 更新文件权限
【发布时间】:2014-11-02 00:20:17
【问题描述】:

如何在 Windows 7 上使用 git-bash 更新文件权限?

我尝试了以下方法但没有成功:

$ ls -al scripts/script.sh
-rw-r--r--    1 myUid   Administ       70 Sep  8 11:24 scripts/script.sh

$ git update-index --chmod=+x scripts/script.sh

$ ls -al scripts/script.sh
-rw-r--r--    1 myUid   Administ       70 Sep  8 11:24 scripts/script.sh

$ chmod +x scripts/script.sh

$ ls -al scripts/script.sh
-rw-r--r--    1 myUid   Administ       70 Sep  8 11:24 scripts/script.sh

【问题讨论】:

    标签: git cygwin windows-7-x64 git-bash


    【解决方案1】:

    很可能您的脚本开头没有“shebang”。所以 Windows 不知道应该使用哪个二进制文件来运行脚本。因此权限会被默默地忽略。

    例如:

    #!/usr/bin/sh
    

    #!/usr/bin/bash
    

    看起来 git-bash 会自动检测到它,因为即使没有 chmod 命令,也可以设置可执行属性。此外,无法使用 chmod 禁用它。

    【讨论】:

      【解决方案2】:

      如果您在 Windows 环境中更新部署到 linux 文件系统的脚本,即使它们被允许在本地运行,您仍可能会发现自己需要在推送之前授予执行权限。

      来自Change file permissions when working with git repo's on windows上的这篇文章:

      1. 在 Windows 上打开像 git-bash 这样的 bash 终端
      2. 导航到您要授予执行权限的 .sh 文件
      3. 使用以下命令检查现有权限:

        git ls-files --stage 
        

        应该返回类似 100644

      4. 使用以下命令更新权限

        git update-index --chmod=+x 'name-of-shell-script'
        
      5. 再次检查文件权限

        git ls-files --stage 
        

        应该返回类似 100755

      6. 提交更改并推送!

      【讨论】:

      • 有用的答案。你能告诉我你用来标签 git bash 窗口的这个软件是什么吗?
      • @LahiruChandima, Windows Terminal - 强烈推荐。
      • 这正是我正在寻找的答案。在我的例子中,git ls-files --stage 显示它们已经是 644,并且 MSYS 正在通过启发式方法将其中一些标记为可执行。
      【解决方案3】:

      您可能在 Windows 上使用 NTFS 或 FAT32,而这些文件系统不支持 executable 权限。相反,cygwin looks at the file name and contents to determine whether it's executable:

      如果文件名以 .bat、.com 或 .exe 结尾,或者其内容以 #! 开头,则认为文件是可执行的。

      因此,您应该确保 bash 文件以 shebang 开头(例如 #!/bin/bash)。然后,您应该可以只执行该文件,而忽略ls 的权限输出。

      【讨论】:

      • 什么是shebang
      • @EdwardBlack 类似#/bin/bash,我相应地澄清了这个问题。
      • .CMD 怎么样? :-)
      猜你喜欢
      • 2011-09-22
      • 2011-03-07
      • 2016-10-10
      • 2011-01-08
      • 2018-12-14
      • 2017-01-17
      • 1970-01-01
      • 2012-01-10
      • 2013-12-30
      相关资源
      最近更新 更多