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.

- 134,834
- 32
- 188
- 245

- 55
- 1
- 1
- 5
-
What have you already tried? – MrTux Sep 06 '14 at 12:21
-
How is this related to git? – MrTux Sep 06 '14 at 12:22
-
@MrTux I want to get file encoding with git command. How is related to git? – nmax Sep 06 '14 at 13:18
-
1Note that the `file` command can only guess at a file's encoding. In general, there is no way to perfectly detect file encodings. – ChrisGPT was on strike Sep 06 '14 at 18:54
2 Answers
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.
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 withThe 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:git filter-branch
Then use#!/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
to run this script over and over again, once per commit:git filter-branch
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."git filter-branch --tree-filter /tmp/recode-all-files HEAD

- 1
- 1

- 1,465
- 2
- 28
- 43
-
Yes, I've already found this and this is not I want. I just want to get file encoding with git. Of course I can do it with bash commands as I pointed in question. – nmax Sep 06 '14 at 13:20