简体   繁体   English

防止git checkout覆盖文件

[英]prevent git checkout from overwriting a file

Another dev checked his .rvmrc into the git repo. 另一个开发人员将他的.rvmrc检查到了git repo。 I've since removed it and added it to gitignore, but every time in need to go back in time it overwrites my .rvmrc. 我已经将它删除并添加到gitignore,但每次需要回溯它都会覆盖我的.rvmrc。 I'm on OSX, so I've discovered I can use the OSX file locking mechanism ("Locked" checkbox in Get Info) to stop this happening on my dev machine, but how would this be done on Linux? 我在OSX上,所以我发现我可以使用OSX文件锁定机制(Get Info中的“Locked”复选框)来阻止我在开发机器上发生这种情况,但是如何在Linux上完成呢?

I chown'd it to root and chmod'd it to 444, yet git (run as my normal user), somehow transcends the need for ownership & permission and still overwrites the file. 我知道它要root并chmod到444,但git(以我的普通用户身份运行),不知何故超越了所有权和权限的需要,仍然覆盖了文件。

Ironically, while git's default behavior is to treat untracked files as precious, adding the file to .gitignore tells git that it can trash the file whenever it wants. 具有讽刺意味的是,虽然git的默认行为是将未跟踪文件视为珍贵文件,但将文件添加到.gitignore告诉git它可以随时.gitignore文件。

Entries in .gitignore are more commonly used to indicate generated files, and as such git will overwrite them silently. .gitignore中的条目更常用于表示生成的文件,因此git将以静默方式覆盖它们。 Entries in .git/info/exclude are perhaps supposed to be used for local versions of configuration files. .git/info/exclude中的条目可能应该用于配置文件的本地版本。

Please read this discussion in its entirety, as there seems to be some confusion as to the difference between the two. 请完整阅读本讨论 ,因为两者之间的差异似乎有些混乱。 I just tried this out, and entries in .git/info/exclude are protected when you go back in time, producing "error: Untracked working tree file '.rvmrc' would be overwritten by merge." 我刚尝试了这个,当你回到过去时, .git/info/exclude中的条目受到保护,产生“错误:未经跟踪的工作树文件'.rvmrc'将被合并覆盖。” This different behavior between the two might not be deliberate, according to Junio C Hamano. 根据Junio C Hamano的说法,两者之间的这种不同行为可能不是故意的。 However, if you can remove the entry from .gitignore and move it to .git/info/exclude , you might be able to make this work, realizing that the latter is not versioned or shared between repositories and would require manual cooperation with your colleague. 但是,如果您可以从.gitignore删除该条目并将其移至.git/info/exclude ,您可能能够完成此工作,意识到后者未在存储库之间进行版本控制或共享,并且需要与您的同事进行手动协作。

As for the permissions issue, see this: 至于权限问题,请参阅:

$ touch foo
$ ls -l foo
-rw-r--r--  1 josh  staff  0 Oct  1 09:55 foo
$ sudo chown root:wheel foo
$ ls -l foo
-rw-r--r--  1 root  wheel  0 Oct  1 09:55 foo
$ unlink foo
$ ls -l foo
ls: foo: No such file or directory

In Unix, the ability to create or remove a file is based only on the permissions of the directory that contains it. 在Unix中,创建或删除文件的能力仅基于包含它的目录的权限。 Permissions on the file itself are only for modifications to the file (unfortunately in your case). 文件本身的权限仅用于修改文件(不幸的是在您的情况下)。 The rm utility checks permissions on the file as a courtesy, but unlink is lower-level and blindly calls the system call. rm实用程序检查文件的权限是礼貌,但unlink是较低级别并盲目地调用系统调用。

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

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