Skip to content
Versora

Manual

Repository

Legacy encodings and line endings

7 min read

On this page

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.

Supported encodings

Name on the buttonFull name in the menu
UTF-8UTF-8
Windows-1252Windows-1252 (Western European, "ANSI")
ISO-8859-1ISO-8859-1 (Latin-1)
ISO-8859-15ISO-8859-15 (Latin-9, with €)
IBM 850IBM 850 (DOS, Western European)
UTF-16 LEUTF-16 LE (Windows "Unicode")
UTF-16 BEUTF-16 BE

How Versora chooses the encoding

For each file, the first rule that applies wins:

  1. working-tree-encoding in .gitattributes: Git already converts the file and returns UTF-8;
  2. the file's manual choice, made with the encoding button;
  3. the encoding attribute in .gitattributes;
  4. the UTF-16 byte order mark (BOM);
  5. UTF-8, if the content is valid UTF-8;
  6. 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)."

Changing a file's encoding

Where to find it

  • 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.

Step by step

  1. 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.
  2. 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…
  3. 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.

OptionWhere it appliesDefault
File encodingOnly that file, in this repository, on this computerDetect automatically
Repository default for non-UTF-8 filesFiles without a choice of their own whose content isn't valid UTF-8Windows-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.

What Versora does in git

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.").

How to undo

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).

Caveats and messages

  • "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›".

Preferences › Encodings

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."

For the whole team: .gitattributes

The choices in Preferences stay on your computer only. To make them apply to everyone, declare the encoding in the repository's .gitattributes:

.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
  • encoding is only a hint about how to read the file. The content in the repository doesn't change.
  • working-tree-encoding changes 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.

Line ending warning before the commit

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.

Where it appears

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.

The warnings

WarningWhenExplanation 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").

How to fix it

The safest way, for the whole team, is a .gitattributes at the root:

.gitattributes
* text=auto

After creating the file, normalize what's already in the repository (in the terminal):

bash
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."):

CheckboxDefault
Warn about line endings before committingOn
Include LF → CRLF (common on Windows with core.autocrlf=true)Off
  • Preferences › Encodings (id encoding) and Preferences › Operations (id ergoOps): the administrator can hide them or make them read-only with the preferences_hidden and preferences_locked keys. See Policy keys.

See also