Change Git Line Endings

In a similar vein to my previous article, here is a script to change the line endings of all files across all revisions in a Git repository to UNIX line endings.

git filter-branch -f
  --tree-filter 'find . 
    -name .git -prune -o 
    -exec sh -c "file {} | grep -q text" \;
    -exec dos2unix -q {} \;
  ' 

Subversion, by default, stores all files in the repository in the line endings in which they were created. Git, by default, converts all files in the repository to have UNIX newline endings when they are added. If you convert a repository from Subversion to Git using git svn clone then the repository files are copied from Subversion to Git, leaving them not having their original line endings in the Git repository.

After you've run this command, you won't be able to use use the git svn commands to communicate with the Subversion repository any more, and you will need a git push --force-with-lease and everyone else will have to update their repositories, and any commits or working copy changes they've done won't merge. So do this just after you've done a one-time conversion from Subversion to Git, or at a time when everyone in the team has checked in, pushed, and stop working.

P.S. I recently created a nerdy privacy-respecting tool called When Will I Run Out Of Money? It's available for free if you want to check it out.

This article is © Adrian Smith.
It was originally published on 13 Apr 2018
More on: VCS