你如何处理 git 中的文件所有权?

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

How do you deal with file ownership in git?

gitpermissions

提问by Shawn J. Goff

I'm working on an embedded Linux project. Our build process makes an image that gets flashed to a device. Many of our files require root ownership for the system to work correctly. I ran into a problem when I tried to pull and some of those files were modified - git couldn't write those files, so did reset hard and did sudo pull. Then when I switched branches, it said "unable to unlink..." for all those files, but it switched branches anyway, then when I tried to switch back to the branch it wouldn't let me because I had local changes.

我正在研究一个嵌入式 Linux 项目。我们的构建过程制作了一个闪存到设备的图像。我们的许多文件都需要 root 所有权才能让系统正常工作。当我尝试拉取时遇到了一个问题,其中一些文件被修改了 - git 无法写入这些文件,所以确实重新设置并执行了 sudo pull。然后当我切换分支时,它对所有这些文件说“无法取消链接......”,但无论如何它切换了分支,然后当我尝试切换回分支时它不会让我因为我有本地更改。

So I'm not doing something right; what is the correct way to deal with this?

所以我没有做正确的事情;处理这个问题的正确方法是什么?

采纳答案by Karmastan

I would structure your system so that the source files don't care what owner they are. You can check these in and out of git without worrying what permissions they have or who the owner is (especially since "owner" isn't meaningful across all the systems a git deployment is likely to serve).

我会构建您的系统,以便源文件不关心它们的所有者。您可以在 git 中检入和检出这些,而无需担心他们拥有什么权限或所有者是谁(特别是因为“所有者”在 git 部署可能服务的所有系统中都没有意义)。

When you want to generate the embedded image, copy everything to a new directory and thenset the permissions as you need.

当您要生成嵌入图像时,请将所有内容复制到新目录,然后根据需要设置权限。

回答by Ether

To build on Karmastan's answer, the magic words here are "build script".

以 Karmastan 的回答为基础,这里的神奇词是“构建脚本”。

Files in git don't have to look like the deployment versions. You don't deploy .c files -- you compile them first. Likewise some config files can go through a build process before being deployed/installed, also.

git 中的文件不必看起来像部署版本。您不部署 .c 文件——您首先编译它们。同样,一些配置文件也可以在部署/安装之前完成构建过程。

回答by igorw

The easiest way (if it's possible) would be to not do any operations (cloning etc) as root, because that leads to the other user not being able to work with the files.

最简单的方法(如果可能的话)是以 root 身份不进行任何操作(克隆等),因为这会导致其他用户无法使用这些文件。

An alternative would be using git init --sharedto set up shared (group or all) permissions for the repository, followed by a git remote add origin http://host/repo.gitand a git pull origin master. Which is basically a clone with less strict permissions.

另一种方法是使用git init --shared为存储库设置共享(组或所有)权限,后跟 agit remote add origin http://host/repo.git和 a git pull origin master。这基本上是一个权限不太严格的克隆。

回答by wilhelmtell

I'm not sure I understand why some files must be chowne'd root. Intuitively I'd guess your problem is the dependence on the owner, not that Git doesn't store ownership. What you cando is chownin you your build.

我不确定我是否理解为什么某些文件必须是chowne根目录。直觉上我猜你的问题是对所有者的依赖,而不是 Git 不存储所有权。你可以做的是chown在您的构建。