如何防止 git 更改文件所有权

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15076348/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:13:45  来源:igfitidea点击:

How to keep git from changing file ownership

gitfileownership

提问by user2108258

I have been noticing that when I pull from my github repo on a development server(Red Hat) the ownership of the files change after the pull is completed. The .git file used to be owned by me but then I noticed that it would write files as me and I need it to write files as a different user. So i changed the ownership of the .git directory.

我一直注意到,当我从开发服务器(Red Hat)上的 github 存储库中拉取时,拉取完成后文件的所有权会发生变化。.git 文件曾经归我所有,但后来我注意到它会以我的身份写入文件,而我需要它以不同的用户身份写入文件。所以我改变了 .git 目录的所有权。

I stumbled on to git config core.filemodewhich was true. I since have set to it false. I have not seen a difference after setting this to false. What should I do to keep my files ownership from changing.

我偶然发现了 git config core.filemode,这是真的。从那以后,我将其设置为 false。将此设置为 false 后,我没有看到任何区别。我应该怎么做才能防止我的文件所有权发生变化。

This doesn't happen to me locally.

这不会发生在我本地。

回答by nafg

If it suffices to preserve the group, you can set the setgid flag on the directories. See http://en.wikipedia.org/wiki/Setuid

如果足以保留组,您可以在目录上设置 setgid 标志。见http://en.wikipedia.org/wiki/Setuid

Setting the setgid permission on a directory ("chmod g+s") causes new files and subdirectories created within it to inherit its group ID, rather than the primary group ID of the user who created the file (the owner ID is never affected, only the group ID). Newly created subdirectories inherit the setgid bit. Thus, this enables a shared workspace for a group without the inconvenience of requiring group members to explicitly change their current group before creating new files or directories. Note that setting the setgid permission on a directory only affects the group ID of new files and subdirectories created after the setgid bit is set, and is not applied to existing entities. Setting the setgid bit on existing subdirectories must be done manually, with a command such as the following:

[root@foo]# find /path/to/directory -type d -exec chmod g+s '{}' \;

在目录(“chmod g+s”)上设置 setgid 权限会导致在其中创建的新文件和子目录继承其组 ID,而不是创建文件的用户的主组 ID(所有者 ID 永远不会受到影响,只有组 ID)。新创建的子目录继承 setgid 位。因此,这可以为组提供共享工作区,而无需在创建新文件或目录之前要求组成员显式更改其当前组。请注意,对目录设置 setgid 权限仅影响设置 setgid 位后创建的新文件和子目录的组 ID,不适用于现有实体。在现有子目录上设置 setgid 位必须手动完成,使用如下命令:

[root@foo]# find /path/to/directory -type d -exec chmod g+s '{}' \;

回答by jthill

Git isn't a deployment server. It writes files as whoever's writing the files, that would be you. If you need permissions and ownership set, you could cook up a post-checkout hook and have it read a config file or something to decide what to do next.

Git 不是部署服务器。它像写文件的人一样写文件,那就是你。如果您需要权限和所有权设置,您可以创建一个结帐后挂钩并让它读取配置文件或其他内容来决定下一步做什么。