Repository
Legacy encodings and line endings
7 min read
Git compares bytes, not letters. An old file saved in Windows-1252 ("ANSI"), Latin-1 or UTF-16 shows its accented characters replaced by "�" in a program that only reads UTF-8. Worse: line staging done from that text can write the wrong bytes to the file.
Versora reads each file in its own encoding and writes it back in the same encoding. Accented characters show up correctly in the diff, blame, file history, revision viewer and merge request review (when the diff comes from the local Git). Staging, unstaging and discarding lines and hunks write the bytes in the file's encoding: a Windows-1252 file stays Windows-1252.
This page also covers the line ending warning before the commit, which deals with another classic problem of files on Windows: CRLF and LF.
| Name on the button | Full name in the menu |
|---|---|
| UTF-8 | UTF-8 |
| Windows-1252 | Windows-1252 (Western European, "ANSI") |
| ISO-8859-1 | ISO-8859-1 (Latin-1) |
| ISO-8859-15 | ISO-8859-15 (Latin-9, with €) |
| IBM 850 | IBM 850 (DOS, Western European) |
| UTF-16 LE | UTF-16 LE (Windows "Unicode") |
| UTF-16 BE | UTF-16 BE |
For each file, the first rule that applies wins:
working-tree-encodingin.gitattributes: Git already converts the file and returns UTF-8;- the file's manual choice, made with the encoding button;
- the
encodingattribute in.gitattributes; - the UTF-16 byte order mark (BOM);
- UTF-8, if the content is valid UTF-8;
- the repository default for non-UTF-8 files (Windows-1252, if you didn't choose another one).
The button's tooltip says which rule applied, for example:
- "Read as UTF-8: the content is valid UTF-8.";
- "The content isn't valid UTF-8: read as Windows-1252, the repository default for these files.";
- "Read as IBM 850, the encoding chosen for this file.";
- "Read as ISO-8859-15, as .gitattributes asks (encoding=ISO-8859-15).";
- "Git converts this file from UTF-16LE-BOM (working-tree-encoding in .gitattributes) and returns UTF-8.";
- "Read as UTF-16 LE, from the file's byte order mark (BOM)."
- The encoding button in the diff bar (it shows the encoding's name, for example "Windows-1252"). It also appears in blame and in the merge request review diff.
- Preferences › Encodings.
- Open the file's diff (or blame). The button shows the encoding in use. With "⚠" in front of it, there are bytes that aren't valid UTF-8 and the file was read the wrong way.
- Click the button. The menu has:
- Detect automatically: goes back to the rules above;
- the list of encodings, with ✓ on the chosen one;
- Repository default for non-UTF-8 files › with Windows-1252, ISO-8859-1, ISO-8859-15 and IBM 850;
- Encoding preferences…
- Choose the right encoding. The diff (or blame) is read again right away.
When .gitattributes has working-tree-encoding for the file, the menu shows "Git converts this
file (‹encoding›)" and the choices are disabled: Git is the one doing the conversion.
| Option | Where it applies | Default |
|---|---|---|
| File encoding | Only that file, in this repository, on this computer | Detect automatically |
| Repository default for non-UTF-8 files | Files without a choice of their own whose content isn't valid UTF-8 | Windows-1252 |
The choices are kept in <common .git folder>/versora/encoding.json. They aren't versioned, apply
to all of the repository's worktrees and stay on this computer only.
Git keeps comparing bytes. The app decodes the diff in the chosen encoding and, when you stage or discard lines or hunks, builds the patch back in the original bytes before sending it to Git. The commits don't change format.
Git treats a UTF-16 file without working-tree-encoding as binary. For that file, Versora itself
builds the diff and applies line staging, writing it back in UTF-16 with the same BOM ("Git treats
this file as binary: Versora builds the diff and stages lines itself, writing back UTF-16 with the
same BOM.").
The encoding choice doesn't change the repository. To go back, choose Detect automatically on the button or Back to automatic in Preferences. Staging and discarding done with the right encoding are undone as usual (see Undo and redo).
- "Some bytes aren't valid UTF-8: choose the correct encoding to stage or discard lines.": while the file is read the wrong way, line staging is refused, so the wrong bytes aren't written.
- "The diff was read as UTF-8, but the file has bytes that aren't valid UTF-8. Choose the file's correct encoding before staging or discarding lines or hunks."
- "The character "‹c›" doesn't exist in ‹encoding›: nothing was written.": the line has a character that the file's encoding can't represent.
- "‹file› changed since the diff was read. Refresh the diff and try again."
- "This file is UTF-16 without working-tree-encoding: stashing lines and applying part of a stash aren't available for it. Stash the whole file instead."
- "Can't blame ‹file› as UTF-16: Git splits lines at the 0x0A byte, and this file has characters that contain that byte. With working-tree-encoding=UTF-16 in .gitattributes, Git treats the file as text."
- "‹file› isn't valid UTF-16." and "Unknown encoding: ‹name›".
The page explains: "Files in legacy encodings (Windows-1252, Latin-1, IBM 850, UTF-16) are decoded in the diff, blame, file history, viewer and merge request review, and staging or discarding lines and hunks writes bytes in the file's encoding. Valid UTF-8 is read as UTF-8; a file with a UTF-16 BOM, as UTF-16."
In the In this repository (‹name›) part:
- Encoding for non-UTF-8 content: the repository default ("Applies to files without a choice of their own whose content isn't valid UTF-8.");
- Files with a chosen encoding: each file with its choice and the Back to automatic button. With none: "No files. Choose a file's encoding with the encoding button in the diff or blame."
With no repository open: "Open a repository to see and change its choices."
The choices in Preferences stay on your computer only. To make them apply to everyone, declare the
encoding in the repository's .gitattributes:
# display only: Git stores the bytes as they are
*.pas encoding=cp1252
# Git stores UTF-8 in the repository and converts in the working copy
*.ps1 working-tree-encoding=UTF-16LE-BOM
encodingis only a hint about how to read the file. The content in the repository doesn't change.working-tree-encodingchanges what Git stores: the repository gets UTF-8, and Git converts at checkout and staging time. Agree with your team before turning it on for a file that already exists.
Tip
Commit messages are a different matter: the app always exchanges messages with
Git in UTF-8, and the i18n.commitEncoding setting applies only to them.
On Windows, editors usually save lines with CRLF; on Linux and macOS, with LF. Without a rule in the repository, a file with CRLF goes into the commit that way, and people who use LF see the whole file as changed. Versora checks the staged files before the commit and warns you.
In the Before committing window, which appears when you click Commit and some check has something to say. The line ending warnings come with the "Line endings" source. The warning doesn't block: you can go ahead with Commit anyway.
| Warning | When | Explanation shown |
|---|---|---|
| "‹file› goes into the commit with CRLF" | The commit brings CRLF into the repository. | "With core.autocrlf=‹value› and no .gitattributes normalizing the file, CRLF is stored in the repository and shows up as a change for people using LF. To normalize, use core.autocrlf=true (Windows) or a .gitattributes with "* text=auto"." |
| "‹file› goes into the commit with mixed line endings (CRLF and LF)" | The same file has lines with CRLF and with LF. | "CRLF and LF lines in the same file usually come from copy and paste or from two editors. Git stores them as they are (core.autocrlf=‹value›)." |
| "‹file› has CRLF on disk and will become LF the next time git writes the file" | The commit stores LF, but the file on disk has CRLF. | "The commit stores LF (core.autocrlf=‹value› or the .gitattributes). On a checkout, branch switch or stash, the file on disk changes to LF." |
| "‹file› has LF on disk and will become CRLF the next time git writes the file" | With core.autocrlf=true or eol=crlf, Git will write CRLF to disk. It only appears if you turn on the "Include LF → CRLF" option. | "With core.autocrlf=‹value› (or eol=crlf in .gitattributes), git writes CRLF to disk. The commit doesn't change." |
With several files, the warning names up to six and summarizes the rest ("… and N more").
The safest way, for the whole team, is a .gitattributes at the root:
* text=auto
After creating the file, normalize what's already in the repository (in the terminal):
git add --renormalize .
git commit -m "chore: normalize line endings"
Or, only on your computer, git config --global core.autocrlf true.
Preferences › Operations, Warnings group, Line endings before committing item ("Warns when the commit brings CRLF into the repository or when, given your settings (core.autocrlf and .gitattributes), the file's line endings will change on disk."):
| Checkbox | Default |
|---|---|
| Warn about line endings before committing | On |
| Include LF → CRLF (common on Windows with core.autocrlf=true) | Off |
- Preferences › Encodings (id
encoding) and Preferences › Operations (idergoOps): the administrator can hide them or make them read-only with thepreferences_hiddenandpreferences_lockedkeys. See Policy keys.
- Diff
- Blame
- Checks before commit and push
- Official documentation: gitattributes (opens another site)