If I delete a file inside git repo, then re-create it with same content, my Ubuntu says
On branch main nothing to commit, working tree clean
But in Windows or Mac, git diff will show every line in the file edited, even though nothing inside the file changed.
Why is this and how can we change this behaviour? Is there a git setting for this?
I think git should only track file content changes, note some disk level metadata to see if the file was re-created, but it's content not edited. (plus maybe exec bit etc..)
Delete the file inside git repo, restore the file back and see git diff report "no changes" because nothing changed in the file, it is exactly the same and program code works the same still.
Windows and UNIX systems (i.e. Linux/MacOS) use a difference sequence for line-endings.
Windows uses CR+LF (/r/n), whereas UNIX systems use LF (\n) only.
This means if you load a file in Windows, it will likely be re-saving the file with the CR+LF line endings, which causes git diff
to say changes have been made.
You can use a .gitattributes
file to renormalise - https://git-scm.com/docs/gitattributes#_end_of_line_conversion
$ echo "* text=auto" >.gitattributes
$ git add --renormalize .
$ git status # Show files that will be normalized
$ git commit -m "Introduce end-of-line normalization"