Sub topic 8
Renormalizing an existing repository
This chapter assumes you can commit, branch and merge in Git. It also assumes you have already written a .gitattributes file with text=auto and any eol rules. Adding those rules does not rewrite files that are already committed. This chapter covers how to apply them to existing history safely.
Why existing files stay unchanged
Git applies attribute rules when it stages a file (working tree to index) and when it checks one out. A tracked file that you have not touched is not re-staged, so its CRLF endings stay in the repository. The mismatch appears later. Someone edits one line, Git converts the whole file, and the diff shows every line as changed.
git add --renormalize fixes this. It re-stages every tracked file under the given path, applies the current attribute rules, and stages only the files whose stored content would change. It requires Git 2.16 or later.
The procedure
- Start from a clean state. Commit or stash your work, and make sure the branch is up to date with the remote.
- Commit the
.gitattributesfile on its own. - Renormalize and review the result.
- Commit the renormalization separately.
# 1. Confirm nothing else is pending
git status
# 2. Commit the rules first
git add .gitattributes
git commit -m "Add .gitattributes"
# 3. Re-apply the rules to every tracked file
git add --renormalize .
# 4. See which files changed
git status
git diff --cached --stat
# 5. Commit the result on its own
git commit -m "Normalize line endings"Use two commits so reviewers can see the rule change and the mechanical rewrite separately. The second commit can be large, but it contains only line-ending changes.
Check the staged changes
Before committing, confirm that only line endings changed. A whitespace-aware diff should show nothing for most files:
git diff --cached --ignore-space-at-eol --statIf a binary file appears in git status, the rules misclassified it. Add binary for that pattern, run git reset on the file, and renormalize again. Check suspicious paths with git check-attr -a -- path/to/file.
Refreshing working trees
Renormalizing changes what is stored in the repository. It does not necessarily change the files in your working directory. After the commit, you may still have CRLF files on disk. Teammates get the new content when they pull, but only for files that changed.
To rewrite the working tree from the repository, first make sure everything is committed. Then run:
git rm --cached -r --quiet .
git reset --hardKeeping blame useful
A mass line-ending commit makes git blame attribute every line to the normalization commit. Record the commit in an ignore file and tell Git to use it:
git log -1 --format=%H > .git-blame-ignore-revs
git config blame.ignoreRevsFile .git-blame-ignore-revsAdd a comment line starting with # above the hash if you want to explain it. Each person must run the git config line once, although many hosting services read the file by name automatically.
Branches in flight
Open branches that edit the same files can conflict, because their ends of the lines differ from the new base. The simplest approach is to merge the updated main branch into each branch. Git merges with the same normalization rules applied, so most conflicts disappear. If conflicts remain, run git add --renormalize . on the branch, commit, and merge again.
Exercise
After you add * text=auto to .gitattributes and commit it, git status still shows a clean tree and old CRLF files remain in the repository. What command re-applies the rules, and why should you commit the result separately?
Show answer
Run git add --renormalize ., which re-stages tracked files under the current rules. Committing the result separately keeps the large line-ending-only change apart from the rule change and other work, so it is easy to review and to list in .git-blame-ignore-revs.
Exercise
You run git add --renormalize . and git status lists logo.png as modified. What does this indicate, and what do you do?
Show answer
Git treated the PNG as text and converted it. Unstage it with git reset logo.png, add *.png binary to .gitattributes, confirm with git check-attr -a -- logo.png, and renormalize again.