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

本文翻译自:How to create file execute mode permissions in Git on Windows?

I use Git in Windows, and want to push the executable shell script into git repo by one commit. 我在Windows中使用Git,并希望通过一次提交将可执行的Shell脚本推送到git repo中。

Usually I need to do two steps ( git commit ). 通常我需要做两个步骤( 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

How can I combine these two steps into one step? 如何将这两个步骤合并为一个步骤? git configuration? git配置? windows command? Windows命令?

Remind : Two answers are good, git add --chmod=+x file is supported in new git version 提醒 :两个答案都不错,新的git版本支持git add --chmod=+x file

Reference: see question in Git file permissions on Windows for second commit 参考:请参阅Windows上Git文件权限中的问题以进行第二次提交


#1楼

参考:https://stackoom.com/question/1T0s6/如何在Windows上的Git中创建文件执行模式权限


#2楼

There's no need to do this in two commits, you can add the file and mark it executable in a single commit: 无需在两次提交中执行此操作,您可以添加文件并在一次提交中将其标记为可执行文件:

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

As you note, after adding, the mode is 0644 (ie, not executable). 如您所述,添加后,模式为0644(即,不可执行)。 However, we can mark it as executable before committing: 但是,我们可以在提交之前将其标记为可执行文件:

C:\Temp\TestRepo>git update-index --chmod=+x foo.sh

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

And now the file is mode 0755 (executable). 现在该文件的模式为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

And now we have a single commit with a single executable file. 现在,我们有了一个带有单个可执行文件的提交。


#3楼

Indeed, it would be nice if git-add had a --mode flag 的确,如果git-add具有--mode标志,那会很好

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

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

That makes the all process quicker, and works even if core.filemode is set to false. 这样可以使所有过程更快,并且即使core.filemode设置为false也可以。

See commit 4e55ed3 (31 May 2016) by Edward Thomson ( ethomson ) . 参见Edward Thomson( ethomson )的 commit 4e55ed3 (2016年5月31日
Helped-by: Johannes Schindelin ( dscho ) . 帮助人: Johannes Schindelin( dscho
(Merged by Junio C Hamano -- gitster -- in commit c8b080a , 06 Jul 2016) (由Junio C gitster - gitstercommit c8b080a中合并 ,2016年7月6日)

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

The executable bit will not be detected (and therefore will not be set) for paths in a repository with core.filemode set to false, though the users may still wish to add files as executable for compatibility with other users who do have core.filemode functionality. 可执行位将不与仓库的路径进行检测(因此不会设置) core.filemode设置为false,虽然用户仍然可能希望添加文件作为可执行文件与谁有其他用户的兼容性core.filemode功能。
For example, Windows users adding shell scripts may wish to add them as executable for compatibility with users on non-Windows. 例如,添加外壳脚本的Windows用户可能希望将它们添加为可执行文件,以与非Windows用户兼容。

Although this can be done with a plumbing command ( git update-index --add --chmod=+x foo ), teaching the git-add command allows users to set a file executable with a command that they're already familiar with . 尽管这可以通过管道命令( git update-index --add --chmod=+x foo )来完成,但是教导git-add命令允许用户使用他们已经熟悉的命令来设置可执行文件


#4楼

If the files already have the +x flag set, git update-index --chmod=+x does nothing and git thinks there's nothing to commit, even though the flag isn't being saved into the repo. 如果文件已经设置了+ x标志,则git update-index --chmod=+x不执行任何操作,即使未将标志保存到存储库中,git也认为没有要提交的内容。

You must first remove the flag, run the git command, then put the flag back: 您必须首先删除该标志,运行git命令,然后将该标志放回原处:

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

then git sees a change and will allow you to commit the change. 然后 git看到更改,并允许您提交更改。


#5楼

The note is firstly you must sure about filemode set to false in config git file, or use this command: 注意事项是,首先您必须确保在config git文件filemode设置为false ,或者使用以下命令:

git config core.filemode false

and then you can set 0777 permission with this command: 然后您可以使用以下命令设置0777权限:

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

#6楼

I have no touch and chmod command in my cmd.exe and git update-index --chmod=+x foo.sh doesn't work for me. 我在cmd.exe中没有touchchmod命令,并且git update-index --chmod=+x foo.sh对我不起作用。

I finally resolve it by setting skip-worktree bit: 我最后通过设置skip-worktree位来解决它:

git update-index --skip-worktree --chmod=+x foo.sh
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值