治理代码仓库换行符变更噪声
排查换行符、权限位和编码导致的整文件差异,提供仓库规范化配置、提交步骤与持续检查方案。
完整提示词
<context>
A mixed-OS team sees pull requests where every line of a file changed though nobody edited it. The usual causes are line endings (Windows tools writing CRLF, others LF, with each person's `core.autocrlf` doing something different), executable-bit changes when files pass through Windows or certain file systems (`core.fileMode`), and encoding changes such as a byte-order mark added by an editor. Per-person settings never fix this for good; a committed `.gitattributes` does, followed by one renormalisation commit so the repository content is consistent.
<symptoms>
{{symptoms}}
</symptoms>
Stack: {{stack}}
</context>
<task>
1. **Diagnose** from the symptoms which cause applies, and give the command that confirms it: `git diff --ignore-cr-at-eol --stat` or `git diff -w --stat` (churn disappears means line endings); `git ls-files --eol` to see index and working-tree endings per file; `old mode`/`new mode` lines in `git diff` for file mode; a BOM visible in a hex dump (`head -c 3 <file> | xxd`) for encoding. If the symptoms do not match any cause, say what output to paste and stop.
2. **Write the .gitattributes** for this stack:
- `* text=auto eol=lf` as the default (or `* text=auto` if some tools need CRLF in the working tree);
- explicit `eol=crlf` for files Windows tools require with CRLF (`*.bat`, `*.cmd`, often `*.sln`, `*.ps1` if the team's tools need it);
- explicit `eol=lf` for shell scripts and anything run in Linux containers (`*.sh`, `Dockerfile`);
- `binary` for binary types in the repo (images, fonts, archives, `*.dll`), and nothing marked text that is not text.
3. **Renormalise once,** in its own commit, on a quiet moment agreed with the team: make sure everyone has pushed; `git add --renormalize .`; review `git status` and `git ls-files --eol`; commit as "Normalise line endings" with no other changes. Add the commit hash to `.git-blame-ignore-revs` and show `git config blame.ignoreRevsFile .git-blame-ignore-revs` so blame skips it.
4. **Open branches:** explain that branches started before the renormalise will conflict on whole files; recommend merging or rebasing onto the normalised main with `-X renormalize` (`git rebase -X renormalize main` or `git merge -X renormalize main`).
5. **Per-machine settings:** with `.gitattributes` in place, recommend `core.autocrlf false` on all machines (or `input` on macOS and Linux) so personal settings do not fight the file; editor settings via `.editorconfig` (`end_of_line`, `charset`, `insert_final_newline`); for file mode churn, `git config core.fileMode false` on affected machines and `git update-index --chmod=+x <file>` to set the executable bit deliberately; for BOMs, `charset = utf-8` in `.editorconfig`.
6. **Prevent recurrence:** a CI check that fails on CRLF in LF-only files or on mixed endings, and `.editorconfig` committed.
</task>
<constraints>
- Do not recommend rewriting history to fix line endings; one forward commit is enough.
- Do not mark a file type as text or binary unless it appears in the stack or the symptoms; mark guesses with `# check:`.
- Warn that the renormalise commit touches many files and should not be mixed with real changes.
- If no stack is given, write a minimal .gitattributes and list the file types to add.
</constraints>
<output_format>
## Diagnosis
The cause, the evidence, and the confirming command.
## .gitattributes
One commented code block.
## Renormalise once
Numbered commands, including the blame-ignore step and the note on open branches.
## Per-machine settings
Commands per OS, and an `.editorconfig` snippet.
## Check
Commands to verify (`git ls-files --eol`, a fresh clone on Windows shows no changes) and the CI check idea.
</output_format>可替换内容
- {{symptoms}}
- {{stack}}
分类与标签
编程开发、版本控制、跨平台协作、换行符规范、仓库规范化