Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make git consider .strings files as text file instead of binary file

Tags:

git

Every time we make changes to the .strings files (for iOS localization) from different branches, we will have conflicts.

The reason seems to be that git consider the .strings as binary files instead of text files. Sometimes it is a little annoying because git won't help merging the changes automatically, again, because they are considered as in binary format.

Is there any git settings that will force it to consider a file as text file.

I notice that both SourceTree and GitLab won't be able to tell that it is text file. But XCode itself will be able to show us the diff. Not sure this is related or not.

enter image description here

like image 985
Yuchen Avatar asked Mar 24 '16 17:03

Yuchen


People also ask

Can git diff binary files?

Any binary format can be diffed with git, as long as there's a tool which converts the binary format to plain text. One just needs to add the conversion handlers and attributes in the same way.

Is a binary file instead of a text file?

While both binary and text files contain data stored as a series of bits (binary values of 1s and 0s), the bits in text files represent characters, while the bits in binary files represent custom data. While text files contain only textual data, binary files may contain both textual and custom binary data.

How does Git deal with binary files?

Git can usually detect binary files automatically. No, Git will attempt to store delta-based changesets if it's less expensive to (not always the case). Submodules are used if you want to reference other Git repositories within your project.

Can I upload binary files to GitHub?

The only part of the GitHub web interface which allows you to push a binary (or any other file you want) is when creating a release.


4 Answers

Create a .gitattributes file at the root of your repo with this contents:

*.strings diff

More information: https://git-scm.com/docs/gitattributes

like image 80
Jonathan.Brink Avatar answered Oct 06 '22 21:10

Jonathan.Brink


None of the solutions worked for me, so I did some more research and at this link I found out this:

  • in your project main directory, create or edit a .gitattributes file adding this line:

    *.strings diff=localizablestrings
    
  • in the .gitconfig file in your home directory, add these lines:

    [diff "localizablestrings"]
    textconv = "iconv -f utf-16 -t utf-8"
    

This did the trick.

like image 22
il3v Avatar answered Oct 06 '22 19:10

il3v


After trying Jonathan or Slawomir's solution, I am still not able to see the diff properly. They are still being considered as binary file.

I finally figure out that the our localizable.strings file are not in UTF-8 encoding but in UTF-16 encoding. After changing the encoding back to UTF-8, it works fine.

Is it okay to change the encoding? Yes, it is. It is actually recommended by Apple to use UTF-8.

Note: It is recommended that you save strings files using the UTF-8 encoding, which is the default encoding for standard strings files. Xcode automatically transcodes strings files from UTF-8 to UTF-16 when they’re copied into the product.

[reference]

How do you know what encoding that file is? See this for command line and this for Xcode:

file -I vol34.tex 

See comments below, you may have to change the encoding of the file manually:

iconv -f UTF-16 -t UTF8 file.strings
like image 13
Yuchen Avatar answered Oct 06 '22 21:10

Yuchen


Please add in your project root directory file .gitattributes with content:

*.strings  text

Now git will recognize your files .strings as text files.

like image 1
Slawomir Jaranowski Avatar answered Oct 06 '22 20:10

Slawomir Jaranowski