如何在Windows上的Git中创建文件执行模式权限?


377

我在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 add --chmod=+x file新的git版本支持

参考:请参阅Windows上Git文件权限中的问题以进行第二次提交


10
使用git 2.9.x / 2.10(2016年第三季度),git add --chmod=+x实际上是可能的。请参阅以下我的答案感谢Edward Thomson
VonC

5
值得更新选择的git add --chmod=+x版本的答案
mikemaccana

Answers:


589

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

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

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


147

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

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

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

这样可以加快所有过程,即使将core.filemode其设置为false也可以。

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

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

可执行位不会被检测到(并因此将不设置)用于在存储库的路径core.filemode设置为false,虽然用户可能仍然希望添加文件可执行与谁其他用户的兼容性core.filemode 功能。
例如,添加外壳脚本的Windows用户可能希望将它们添加为可执行文件,以与非Windows用户兼容。

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


22

如果文件已经设置了+ x标志,git update-index --chmod=+x则什么也不做,并且git认为没有要提交的内容,即使该标志没有保存到仓库中也是如此。

您必须首先删除该标志,运行git命令,然后将该标志放回原处:

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

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


9

注意事项是,首先必须确保在config git文件中filemode设置为false,或使用以下命令:

git config core.filemode false

然后您可以使用以下命令设置0777权限:

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

3

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

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

git update-index --skip-worktree --chmod=+x foo.sh
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.