'How do I determine file encoding?

Is there a git command that returns file encoding like file in Linux? That completely describes my problem. I tried searching Google but found nothing.



Solution 1:[1]

Git itself has no idea of the encoding of a file (stored as a blob, meaning as an arbitrary binary data).
See "What is the format of a git “blob”?".

The command file can still be used after a git checkout.
Or piped after a git show to read the content of a specific file, e.g.:

$ git show @~2:README.md | file -
/dev/stdin: ASCII text

tells that the file ./README.md 2 commits ago had an ASCII encoding - notice the last dash (-) denoting STDIN.

While this command:

$ git show :README.md | file -
/dev/stdin: Unicode text, UTF-8 (with BOM) text, with CRLF line terminators

tells that the same file staged in git's 'index' is gonna be Windows encoded.

Solution 2:[2]

If you are trying to convert line endings (CRLF (Windows) to Linux Standards or else) only, you can try something like this, answered by @VonC here.

Or if you want to convert the encoding from files (i.e.: ISO-8859-1 to UTF-8) and you are a linux user, you could try this, answered by @Celada

You can do this with
git filter-branch
The idea is that you have to change the encoding of the files in every commit, rewriting each commit as you go. First, write a script that changes the encoding of every file in the repository. It could look like this:

    #!/bin/sh
    find . -type f -print | while read f; do
        mv -i "$f" "$f.recode.$$"
        iconv -f iso-8859-1 -t utf-8  "$f"
        rm -f "$f.recode.$$"
    done
Then use
git filter-branch
to run this script over and over again, once per commit:
git filter-branch --tree-filter /tmp/recode-all-files HEAD
where /tmp/recode-all-files is the above script. Right after the repository is freshly upgraded from CVS, you probably have just one branch in git with a linear history back to the beginning. If you have several branches, you may need to enhance the git filter-branch command to edit all the commits."

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman
Solution 2 Community