简体   繁体   English

文件所有权不会停留在git分支更改上

[英]file ownership doesn't stay on git branch change

Ok so I have an odd problem. 好吧,我有一个奇怪的问题。

I have a git project with several files. 我有几个文件的git项目。 I accidently created, added and committed a file with the root user as the file owner rather than the user which I have set up as the owner for the rest of the project. 我意外地创建,添加并提交了一个文件,该文件的root用户是文件所有者,而不是我已设置为项目其余部分的所有者的用户。 I merged this file so that it is in both my master branch as well as in my dev branch. 我合并了这个文件,使其既在我的master分支中,又在我的dev分支中。 In both branches it reads that root is the owner of this file. 在两个分支中,它都读取root是此文件的所有者。

So with my master branch checked out, I ran chown user:user file.txt to set the file to the usual user for the rest of the project. 因此,在我的master分支签出后,我运行了chown user:user file.txt以将文件设置为项目其余部分的普通用户。 After doing an ls -l I see that the file now shows that "user" is now the owner and group. 执行ls -l我看到该文件现在显示“用户”现在是所有者和组。 Great! 大!

Now if I go and checkout the dev branch and run an ls -l it shows the file is owned by root again. 现在,如果我去检出dev分支并运行ls -l则表明文件再次由root拥有。 And if I checkout the master branch again it now shows that the file owner has changed back to being the root user rather than "user". 如果我再次检出master分支,它现在表明文件所有者已更改回root用户而不是“ user”。

I tried many things, but every time I checked out a branch that contained the file, the user was always set as "root" no matter how hard I tried to set it as "user". 我尝试了很多事情,但是每次我检出包含该文件的分支时,无论我多么努力地将其设置为“ user”,都始终将用户设置为“ root”。

Am I doing something wrong? 难道我做错了什么? git shouldn't be storing the owner of the file, but yet it seems to be ignoring my ownership changes. git不应存储文件的所有者,但是它似乎忽略了我的所有权更改。

Thanks. 谢谢。

It is not possible to create a file that's owned by root, unless you're root yourself[1]. 除非您自己是root用户,否则无法创建root用户拥有的文件[1]。 This is a fundamental security issue that predates even Linux. 这是一个根本的安全问题,甚至早于Linux。

Stop using the root userid when working with your git repository, and the problem will go away. 在使用git存储库时,请停止使用root用户ID,问题将消失。

Do a recursive chown -R on your entire git repository. 在整个git存储库上执行递归chown -R。 Then always execute git commands while logged in as the correct user. 然后,始终以正确的用户身份登录时执行git命令。

[1] Yes, you can rename root-owned files if you have write permissions on the directory. [1]是的,如果您对该目录具有写权限,则可以重命名root拥有的文件。 But git doesn't do that, when checking out files. 但是当签出文件时,git不会这样做。

It turns out that I had to push a commit to the remote repository for the ownership changes to stick. 事实证明,必须将提交推送到远程存储库才能保留所有权更改。 Not sure why this was. 不知道为什么会这样。 And yes I know don't use the root user etc etc. It was a stupid little mistake, one which I was attempting to remedy. 是的,我知道不要使用root用户等。这是一个愚蠢的小错误,我正在尝试纠正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM