Git says “Binary files a… and b… differ” on for *.reg files

后端 未结 4 1606
刺人心
刺人心 2021-02-19 14:47

Is there a way to force Git in to treating .reg files as text? I am using Git to track my windows registry tweaks and Windows uses .reg for these files

相关标签:
4条回答
  • 2021-02-19 14:52

    Convert .reg files from utf16 to utf8 by opening each .reg file in notepad and saving as Encoding UTF-8.

    0 讨论(0)
  • 2021-02-19 15:02

    To tell git to explicitly diff a filetype, put the following in a .gitattributes file in your repository’s root directory:

    *.reg diff
    
    0 讨论(0)
  • 2021-02-19 15:02

    Create one utf16toascii.py:

    #!/usr/bin/env python3
    import sys
    data = open(sys.argv[-1]).read()
    ascii = data.decode('utf-16').encode('ascii', 'replace')
    sys.stdout.write(ascii)
    

    Then in bash do:

    $ echo "*.reg diff=utf16strings" >> .gitattributes
    $ git config --global diff.utf16strings.textconv /path/to/utf16toascii.py
    

    And you're good to diff registry files, as well as Xcode .strings files, or any other utf-16 file.

    0 讨论(0)
  • 2021-02-19 15:08

    Git is treating your registry export files as binary files because they have NULs. There is no good way to diff or merge general binary files. A change of one byte can change the interpretation of the rest of the file.

    There are two general approaches to handling binary files:

    1. Accept that they're binary. Diffs aren't going to be meaningful, so don't ask for them. Don't ever merge them, which means only allowing changes on one branch. In this case, this can be made easier by putting each tweak (or set of related tweaks in a separate file, so there's fewer possible ways differences will happen in one file.

    2. Store the changes as text, and convert/deconvert to these binary forms.

    Even though these "text" files, the UTF-16 encoding contains NULs. There appear to be no non-ASCII bits however. Can you convert them to ASCII (or UTF-8, which will be ASCII if there are no extended characters)?

    0 讨论(0)
提交回复
热议问题