MD XF
MD XF

Reputation: 8129

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?

Upvotes: 1

Views: 146

Answers (3)

Ingo Karkat
Ingo Karkat

Reputation: 172688

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.

Upvotes: 0

VonC
VonC

Reputation: 1327324

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.

Upvotes: 1

jbu
jbu

Reputation: 16151

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.

Upvotes: 0

Related Questions