如何将chmod权限添加到GIT文件中? [英] How to add chmod permissions to file in GIT?

查看:561
本文介绍了如何将chmod权限添加到GIT文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想git提交一个.sh文件,但希望它可以在另一台服务器上签出同一文件时执行。



有没有办法所以没有手动chmod u + x那个文件在检出文件的服务器上?解析方案

根据 Git文档,您可以设置或删除可执行文件标记在任何跟踪的文件上,在任何系统上(是的,这在Windows上都可以使用)使用 update-index 子命令。



要将文件设置为可执行文件,请使用

  git update-index --chmod = + x path / to /文件

并删除可执行标志,请使用

  git update-index --chmod = -x path / to / file 

如果这看起来像常规的unix权限系统,实际上不是。事实上,Git只允许设置或取消设置可执行文件( x )的许可权限,并且只允许在文件上执行。



2.9,您可以创建一个文件并使用单个命令添加该标志:

  git add --chmod = + x path / to / file 

在底层,Git为每个文件维护一个特殊的文件模式: 100644 为常规文件, 100755 为可执行文件。您可以使用 ls-file 子命令以 - stage 选项可视化这些模式:

  $ git ls-files --stage 
100644 aee89ef43dc3b0ec6a7c6228f742377692b50484 0 .gitignore
100755 0ac339497485f7cc80d988561807906b2fd56172 0 my_executable_script.sh

默认情况下,Git将尝试遵守文件系统属性并相应地设置git文件模式。您可以通过将 core.filemode 选项设置为false来禁用此功能:

  git config core.fileMode false 

更多信息可以在 Git文档



如果如果没有设置Git filemode,但文件具有正确的文件系统标志,请尝试删除模式并重新设置:

  git update-index --chmod = -x path / to / file 
git update-index --chmod = + x path / to / file


I want to git commit a .sh file, but want it to be executable when I checkout that same file in another server.

Is there a way to do so without manually chmod u+x that file in the servers that checkout the file?

解决方案

According to Git documentation, you can set or remove the "executable" flag on any tracked file, on any system (yeah, this works on Windows) using update-index sub-command.

To set a file as executable, use

git update-index --chmod=+x path/to/file

and to remove the executable flag, use

git update-index --chmod=-x path/to/file

If this looks like the regular unix permission system, actually it is not. Indeed, Git allows to set or unset only executable (x) permission and only on files.

Starting with Git 2.9, you can stage a file AND add the flag using a single command:

git add --chmod=+x path/to/file

Under the hood, Git maintain a special filemode for each file: 100644 for regular files, 100755 for executable ones. You can visualize these modes using ls-file subcommand, with --stage option:

$ git ls-files --stage
100644 aee89ef43dc3b0ec6a7c6228f742377692b50484 0       .gitignore
100755 0ac339497485f7cc80d988561807906b2fd56172 0       my_executable_script.sh

By default, Git will try to honor filesystem attributes and set the git filemode accordingly. You can disable this by setting core.filemode option to false:

git config core.fileMode false

More info can be found on that option in Git docs.

If you fell in the case the Git filemode is not set but the file has correct filesystem flag, try to remove mode and set again:

git update-index --chmod=-x path/to/file
git update-index --chmod=+x path/to/file

这篇关于如何将chmod权限添加到GIT文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆