如何在 GIT 中为文件添加 chmod 权限?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40978921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add chmod permissions to file in GIT?
提问by Henley Chiu
I want to git commit a .sh file, but want it to be executable when I checkout that same file in another server.
我想 git commit 一个 .sh 文件,但希望它在我在另一台服务器上检出同一个文件时是可执行的。
Is there a way to do so without manually chmod u+x that file in the servers that checkout the file?
有没有办法做到这一点,而无需在检出文件的服务器中手动 chmod u+x 那个文件?
回答by Antwane
According to official documentation, you can set or remove the "executable" flag on any tracked file using update-index
sub-command.
根据官方文档,您可以使用update-index
子命令设置或删除任何跟踪文件上的“可执行”标志。
To set the flag, use following command:
要设置标志,请使用以下命令:
git update-index --chmod=+x path/to/file
To remove it, use:
要删除它,请使用:
git update-index --chmod=-x path/to/file
Under the hood
引擎盖下
While this looks like the regular unix files permission system, actually it is not. Git maintains a special "mode" for each file in its internal storage:
虽然这看起来像常规的 unix 文件权限系统,但实际上并非如此。Git 为其内部存储中的每个文件维护一个特殊的“模式”:
100644
for regular files100755
for executable ones
100644
对于常规文件100755
对于可执行文件
You can visualize it using ls-file
subcommand, with --stage
option:
您可以使用ls-file
子命令将其可视化,并带有以下--stage
选项:
$ git ls-files --stage
100644 aee89ef43dc3b0ec6a7c6228f742377692b50484 0 .gitignore
100755 0ac339497485f7cc80d988561807906b2fd56172 0 my_executable_script.sh
By default, when you add a file to a repository, Git will try to honor its filesystem attributes and set the correct filemode accordingly. You can disable this by setting core.fileMode
option to false:
默认情况下,当您将文件添加到存储库时,Git 将尝试遵守其文件系统属性并相应地设置正确的文件模式。您可以通过将core.fileMode
选项设置为 false来禁用此功能:
git config core.fileMode false
Troubleshooting
故障排除
If at some point the Git filemode is not set but the file has correct filesystem flag, try to remove mode and set it again:
如果在某些时候未设置 Git 文件模式但文件具有正确的文件系统标志,请尝试删除模式并重新设置:
git update-index --chmod=-x path/to/file
git update-index --chmod=+x path/to/file
Bonus
奖金
Starting with Git 2.9, you can stage a file AND set the flag in one command:
从 Git 2.9 开始,您可以暂存文件并在一个命令中设置标志:
git add --chmod=+x path/to/file
回答by torek
Antwane's answeris correct, and this should be a comment but comments don't have enough space and do not allow formatting. :-) I just want to add that in Git, file permissions are recorded only1as either 644
or 755
(spelled (100644
and 100755
; the 100
part means "regular file"):
Antwane 的回答是正确的,这应该是评论,但评论没有足够的空间并且不允许格式化。:-)我只想补充一点,在Git中,文件权限被记录只有1为两种644
或755
(拼写(100644
和100755
;该100
部分的意思是“普通文件”):
diff --git a/path b/path
new file mode 100644
The former—644—means that the file should notbe executable, and the latter means that it shouldbe executable. How that turns into actual file modes within your file system is somewhat OS-dependent. On Unix-like systems, the bits are passed through your umask
setting, which would normally be 022
to remove write permission from "group" and "other", or 002
to remove write permission only from "other". It might also be 077
if you are especially concerned about privacy and wish to remove read, write, and execute permission from both "group" and "other".
前者-644-意味着该文件应该不会是可执行的,而后者意味着它应该是可执行的。如何在您的文件系统中变成实际的文件模式在某种程度上取决于操作系统。在类 Unix 系统上,这些位通过您的umask
设置传递,这通常是022
从“组”和“其他”002
中删除写权限,或者仅从“其他”中删除写权限。077
如果您特别关心隐私并希望从“组”和“其他”中删除读取、写入和执行权限,也可能是这样。
1Extremely-early versions of Git saved group permissions, so that some repositories have tree entries with mode 664
in them. Modern Git does not, but since no part of any object can ever be changed, those old permissions bits still persist in old tree objects.
1极早期版本的 Git 保存了组权限,因此某些存储库中包含带有模式的树条目664
。现代 Git 没有,但由于任何对象的任何部分都不能改变,那些旧的权限位仍然存在于旧的树对象中。
The change to store only 0644 or 0755 was in commit e44794706eeb57f2, which is before Git v0.99 and dated 16 April 2005.
仅存储0644或 0755 的更改是在提交e44794706eeb57f2 中,它在 Git v0.99 之前,日期为 2005 年 4 月 16 日。