防止git checkout覆盖文件 [英] prevent git checkout from overwriting a file

查看:1252
本文介绍了防止git checkout覆盖文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一位开发人员将他的.rvmrc检入git回购。我已经删除它并将其添加到gitignore,但每次需要及时回覆它会覆盖我的.rvmrc。我在OSX上,所以我发现我可以使用OSX文件锁定机制(Get Info中的Locked复选框)来阻止这种情况发生在我的开发机器上,但是如何在Linux上完成?



我把它修改为root并将它修改为444,然而git(以我的普通用户身份运行)以某种方式超越了对所有权和所有权的需求。具有讽刺意味的是,虽然git的默认行为是将未跟踪的文件视为珍贵文件,但将文件添加到 .gitignore 告诉git它可以在需要时清理文件。



.gitignore 更常用于指示生成的文件,因此git会默默覆盖它们。 .git / info / exclude 中的条目可能应该用于本地版本的配置文件。



请阅读讨论,因为两者之间的区别似乎存在一些混淆。我刚刚尝试了这一点,并且当你回溯时, .git / info / exclude 中的条目受到保护,产生错误:未经追踪的工作树文件'.rvmrc'会被合并覆盖。根据Junio C Hamano的说法,两者之间的这种不同的行为可能不是故意的。但是,如果您可以从 .gitignore 中移除条目并将其移至 .git / info / exclude ,您可能会能够完成这项工作,意识到后者不是版本库或版本库之间的共享,并需要与您的同事进行手动合作。

至于权限问题,请参阅此:

  $ touch foo 
$ ls -l foo
-rw-r - r-- 1 josh的工作人员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:没有这样的文件或目录

在Unix中,创建或删除文件的功能仅基于包含它的目录的权限。文件本身的权限仅限于修改文件(不幸的是,在您的情况下)。 rm 实用程序会检查文件的权限,但是 unlink 是较低级的,并且会盲目地调用系统调用。


Another dev checked his .rvmrc into the 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. 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?

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.

解决方案

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.

Entries in .gitignore are more commonly used to indicate generated files, and as such git will overwrite them silently. Entries in .git/info/exclude are perhaps supposed to be used for local versions of configuration files.

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." This different behavior between the two might not be deliberate, according to 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.

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. 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.

这篇关于防止git checkout覆盖文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆