【问题标题】:How to create file execute mode permissions in Git on Windows?如何在 Windows 上的 Git 中创建文件执行模式权限?
【发布时间】:2014-03-08 14:38:58
【问题描述】:

我在 Windows 中使用 Git,并希望通过一次提交将可执行的 shell 脚本推送到 git repo 中。

通常我需要做两个步骤(git commit)。

$ vi install.sh
$ git add install.sh  
$ git commit -am "add new file for installation" # first commit
[master f2e92da] add support for install.sh
 1 files changed, 18 insertions(+), 3 deletions(-)
 create mode 100644 install.sh
$ git update-index --chmod=+x install.sh
$ git commit -am "update file permission"        # second commit
[master 317ba0c] update file permission
  0 files changed
  mode change 100644 => 100755 install.sh

如何将这两个步骤合并为一个步骤? git配置? windows命令?

提醒:两个答案都不错,新的git版本支持git add --chmod=+x file

参考:第二次提交请参阅Git file permissions on Windows 中的问题

【问题讨论】:

  • 使用 git 2.9.x/2.10(2016 年第三季度),git add --chmod=+x 实际上是可能的。见my answer below,感谢Edward Thomson
  • 值得将所选答案更新为git add --chmod=+x 版本

标签: git


【解决方案1】:

如果文件已经设置了 +x 标志,git update-index --chmod=+x 什么都不做,git 认为没有什么可以提交,即使标志没有保存到 repo 中。

你必须先移除标志,运行 git 命令,然后将标志放回去:

chmod -x <file>
git update-index --chmod=+x <file>
chmod +x <file>

然后 git 看到更改并允许您提交更改。


提交者所需的 git 配置(信用:Nabi’s answer):

git config core.filemode false

克隆器所需的 git 配置:

git config --global core.autocrlf input

【讨论】:

  • ...太糟糕了!还是这样吗
  • @MrR 是的,仍然如此。我相信这是修复它的最快方法(最少 git fu)。
  • 我这样做了,git 看到了更改,我确实提交并推送到 repo,当我在其他地方使用该文件时,权限被拒绝......知道什么是错的吗?我从 repo 下载文件并检查权限,它没有可执行文件...
  • @mrRobot 检查git configcore.filemode 应该是假的。如果没有,git config core.filemode false(信用Nabi’s answer
  • @Bohemian 也许这对我有帮助:git config --global core.autocrlf input
【解决方案2】:

我的 cmd.exe 中没有 touchchmod 命令 git update-index --chmod=+x foo.sh 对我不起作用。

我终于通过设置skip-worktree位解决了:

git update-index --skip-worktree --chmod=+x foo.sh

【讨论】:

  • 谢谢!几个小时以来,我一直试图在 Windows 上解决这个问题,这对我来说是成功的。
  • 这是我在 Windows 10 下也能正常工作的唯一方法。
【解决方案3】:

注意首先你必须确定在配置git文件中filemode设置为false,或者使用这个命令:

git config core.filemode false

然后你可以用这个命令设置0777权限:

git update-index --chmod=+x foo.sh

【讨论】:

    【解决方案4】:

    确实,如果git-add 有一个--mode 标志会很好

    git 2.9.x/2.10(2016 年第三季度)实际上将允许这样做(感谢Edward Thomson):

    git add --chmod=+x -- afile
    git commit -m"Executable!"
    

    这使得整个过程更快,即使 core.filemode 设置为 false 也能正常工作。

    commit 4e55ed3(2016 年 5 月 31 日)Edward Thomson (ethomson)
    帮助者:Johannes Schindelin (dscho)
    (由 Junio C Hamano -- gitster -- 合并于 commit c8b080a,2016 年 7 月 6 日)

    add:添加--chmod=+x/--chmod=-x选项

    不会检测到可执行位(因此不会被 set) 用于将 core.filemode 设置为 false 的存储库中的路径, 尽管用户可能仍希望将文件添加为可执行文件 与其他确实拥有core.filemode的用户的兼容性 功能。
    例如,添加 shell 脚本的 Windows 用户可能希望将它们添加为可执行文件,以便与非 Windows 用户兼容。

    尽管这可以通过管道命令 (git update-index --add --chmod=+x foo) 完成,教授 git-add 命令允许用户使用他们已经熟悉的命令设置可执行文件

    【讨论】:

      【解决方案5】:

      无需在两次提交中执行此操作,您可以在一次提交中添加文件并将其标记为可执行:

      C:\Temp\TestRepo>touch foo.sh
      
      C:\Temp\TestRepo>git add foo.sh
      
      C:\Temp\TestRepo>git ls-files --stage
      100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0       foo.sh
      

      如您所见,添加后,模式为 0644(即不可执行)。但是,我们可以在提交之前将其标记为可执行:

      C:\Temp\TestRepo>git update-index --chmod=+x foo.sh
      
      C:\Temp\TestRepo>git ls-files --stage
      100755 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0       foo.sh
      

      现在文件是模式 0755(可执行文件)。

      C:\Temp\TestRepo>git commit -m"Executable!"
      [master (root-commit) 1f7a57a] Executable!
       1 file changed, 0 insertions(+), 0 deletions(-)
       create mode 100755 foo.sh
      

      现在我们有了一个带有单个可执行文件的提交。

      【讨论】:

        猜你喜欢
        • 2011-09-22
        • 2014-07-18
        • 2013-01-27
        • 2019-08-18
        • 2012-10-27
        • 2017-11-20
        • 2012-01-10
        • 2012-10-22
        • 1970-01-01
        相关资源
        最近更新 更多