Search code examples
linuxgitgit-commitgit-diff

Git successfully stages file but fails to commit the file


Git successfully stages the files via git add (they show up under "Changes to be committed"), but when I run git commit -m "commit message" and friends, the command silently fails (echo $? displays 1) and my files are still sitting there listed as "Changes to be committed"...

Other similar questions suggested things such as:

  • The .git file potentially being corrupted, with the solution being to re-clone into a separate directory, copy changed files there, and re-attempt the add/commit cycle. This did not help.
  • There being some sort of permissions issue with the files, and to chmod 755 before retrying. This was not relevant for me as the files were already 0755.
  • Restarting the computer. I actually did try this (cuz who knows) but it did not help.

Solution

  • While debugging my problem, I did eventually find someone who pointed out the GIT_TRACE environment variable. I ran:

    export GIT_TRACE=1
    

    and after doing this, I re-ran my failing commit command. Now I saw that before just dying, it was actually trying to run git diff under the hood as part of its commit process. I had seen during my extensive searching through SO that sometimes git diff can fail on large files due to running out of memory but this had not seemed relevant to me since I was trying to git commit. The files in question were in fact large text files though (several MB).

    The solution ultimately was to create a .gitattributes file, with entries like the following (sort of like .gitignore but with the binary keyword at the end):

    problem/file/path/or/pattern binary
    

    This way git would treat the file as a binary file and not run diff (and thus not run out of memory, and not fail to commit). This was an acceptable solution for me since I don't really need change tracking or anything on this particular file.