git: fix CRLF mess retroactively

77 views Asked by At

I mistakenly committed some files with the CRLF line endings.

Now I set core.autocrlf = false and fixed the files locally (in the editor).

Is there a way to fix this (e.g., using some combination of rebase -i &c) so that CRLF completely disappear from history permanently?

1

There are 1 answers

0
VonC On BEST ANSWER

I mentioned:

Using a git rebase -x, you could call a script which would git add --renormalize ., to make sure your local Git config is applied.


Guildenstern also points out in the comments to git test fix

The git test fix subcommand can run command such as a formatter or linter and apply its resulting changes to each provided commit.
For example: git test fix --exec 'cargo fmt --all'.

Preventing merge conflicts

It can be difficult to apply formatter or linter changes by hand (with e.g. git rebase --exec) because it necessarily rebases the descendants of fixed commits.
This oftentimes causes merge conflicts that are about to be resolved by formatting or linting the next commit. (As a workaround, some people try fixing the commits in reverse topological order to minimize the chances of merge conflicts.)

git test fix never produces merge conflicts because it fixes each commit in isolation by replacing its the tree OID directly, and leaves descendant commits otherwise unchanged. (You can do the same thing by hand by calling git amend with the --reparent option)

See git test fix in action here.