简体   繁体   English

为什么 Git 会写入我的文件?

[英]Why does Git write to my files?

I have a few source files in a Git repo, main.c and some others.我在 Git 存储库、 main.c和其他一些源文件中有一些源文件。 I use Vim to edit these files.我使用 Vim 来编辑这些文件。

Say I'm editing main.c .假设我正在编辑main.c I make a change, tell Vim to write-out, then switch to a terminal (without killing Vim) and commit/push.我进行了更改,告诉 Vim 写出,然后切换到终端(不杀死 Vim)并提交/推送。 If I go back to Vim with main.c and try to write-out again, I receive this warning:如果我使用main.c返回 Vim 并尝试再次写出,我会收到以下警告:

WARNING: The file has been changed since reading it!!!
Do you really want to write to it (y/n)?

How does committing/pushing modify my source files?提交/推送如何修改我的源文件? Why?为什么?

It could be a permission issue (git changing the executable bit of the file).这可能是一个权限问题(git 更改了文件的可执行位)。
Try again after git config core.filemode false .git config core.filemode false后重试。

The solution is simple: reopen the file with :e filename .解决方案很简单:使用:e filename重新打开:e filename
Or (if your files are save and have no current modification), run :bufdo e , which will make Vim reopen every buffer.或者(如果您的文件已保存并且当前没有修改),运行:bufdo e ,这将使 Vim 重新打开每个缓冲区。

Can you show us your .gitattributes file?你能告诉我们你的 .gitattributes 文件吗? Are you telling it to change the line endings?你是在告诉它改变行尾吗?

Why don't you try doing a diff and showing us what the differences are.你为什么不尝试做一个差异并向我们展示差异是什么。

The other answers have shown that the write may be due to Git performing a line ending change, or expands configured attributes.其他答案表明写入可能是由于 Git 执行行结束更改或扩展配置的属性。 A simple solution is to一个简单的解决方案是

:set autoread

in Vim, to automatically update the file (as long as you haven't changed it, which based on your use case seems unlikely).在 Vim 中,自动更新文件(只要您没有更改它,根据您的用例似乎不太可能)。

 'autoread' 'ar' boolean (default off) When a file has been detected to have been changed outside of Vim and it has not been changed inside of Vim, automatically read it again.

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

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