简体   繁体   中英

Why does Git write to my files?

I have a few source files in a Git repo, main.c and some others. I use Vim to edit these files.

Say I'm editing main.c . I make a change, tell Vim to write-out, then switch to a terminal (without killing Vim) and commit/push. If I go back to Vim with main.c and try to write-out again, I receive this warning:

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).
Try again after git config core.filemode false .

The solution is simple: reopen the file with :e filename .
Or (if your files are save and have no current modification), run :bufdo e , which will make Vim reopen every buffer.

Can you show us your .gitattributes file? 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. 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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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