Search code examples
gitgithubbinaryfiles

GitHub: Binary files not being copied correctly


I am developing a web application and I have found that GitHub is not copying some of the image files correctly. The files are correctly recognized as binary, and they work correctly on my local machine (Windows, Apache, MySQL, PHP), but when I upload to the server, some of the image files do not work. Google Chrome will not show them, no error or reason why, and Firefox says that the image cannot be displayed because it contains errors.

So, I FTP'd the files in question from the server to my workstation, and using a hex editor, I found that the files that I copied back from the server are one byte short. It seems that right after the PNG marker in the file, the next byte should be a 0x0D, but it has been removed and the rest of the bytes have been shifted over.

So this means that the erroneous files are in the repository.

Now my question is, how do I fix the repository?

EDIT:

After wrangling with this issue a little bit more, I have come to the conclusion that the problem seems to be that GitHub is trying to apply a CRLF -> LF filter to the files in question. I am not sure why it is doing this, but that does seem to be what is going on here.


Solution

  • GitHub itself does not do CRLF tricks. GitHub—well, aside from its web interface anyway—just stores commits for you. Commits are inviolate.

    Git can be told to do CRLF tricks. These occur in two places:

    • At the time files get copied from the index to the work-tree, Git will make any output-side conversions you ask for. In general, Windows users often ask Git to turn LF-only line endings into CRLF line endings.

    • At the time files get copied from the work-tree to the index, Git will make any input-side conversions you ask for. Windows users might ask Git to turn CRLF line endings into LF-only line endings here.

    As a Windows user, you must be careful about which files you tell Git to manipulate this way. You can do this through settings you save in a file named .gitattributes:

    *       text=auto
    *.txt   text
    *.jpg   -text
    

    Any blanket settings you make using core.eol or core.autocrlf or similar are never going to be as careful and precise as specific settings in .gitattributes, so if you are going to use this feature of Git, use a .gitattributes file.

    See the examples in the gitattributes documentation for more.

    Note: "GitHub Desktop" is a program, separate from the Git-repository-hosting site, and that program does do CRLF tricks, presumably the same way that Git does.