I'm working on a coding project and using SourceTree and git for version control. I've discovered that at one point in the past, one of the source code files accidentally contained NUL characters. When this file was saved, its encoding was automatically changed from UTF-8 to ANSI. I did not notice this at the time and committed the file. I have made a couple more commits before noticing the wrong file encoding.
Right now I've changed the file encoding back to UTF-8 and committed the file again. Now I can see changes just fine from this commit onwards. However, SourceTree will not show me the changes in all the previous commits while the file had the ANSI encoding.
I have searched for ways to solve this and tried changing the .gitattributes file according to this answer and this answer, however, SourceTree still refuses to show me the changes.
I have also found that the git differ can be changed according to this answer, but I am not sure how to integrate that change so SourceTree can show me the previous changes.
My Question: is there any way to change the way git diffs the files to support the ANSI encoding or automatically convert all files to utf-8 so I can view the changes in SourceTree for my previous commits again?
I found a solution!
In the repository .gitattributes
file add:
ProblemFile.c diff=utf16_diff
ProblemFile.c set diff
In the global .gitconfig
file add:
[diff "utf16_diff"]
textconv = "iconv -f utf-16 -t utf-8"
Now SourceTree properly shows all previous commits!