git

gitattributesで改行コードの統一

gitattributesはGit にファイルの扱い方を指定する設定ファイルです。特に 改行コードの統一に使われる

以下は.gitattributesのデフォルト設定
/mvnw text eol=lf
*.cmd text eol=crlf
.gitattributesを以下にすることで各種ファイル全てLFで統一

個人的にはこの設定が好みです。特に*.sh text eol=lf は重要。現場あるあるだけど、bashにCRLFが混在してたまたま動作しているものをたまに見かけます。これは本当に危険だからやめましょう。*.shを含まないプロジェクトであってもこの記載をしておいてもよいくらいです。

/mvnw text eol=lf
*.cmd text eol=crlf

*.java text eol=lf
*.xml text eol=lf
*.properties text eol=lf
*.yml text eol=lf
*.md text eol=lf
*.sh text eol=lf
.gitattributes を編集したらrenormalize

Git は「git add」した時初めて .gitattributesの内容を適用するので以下のような状態のファイルに対しては自動では適用されない。そのため手動で「もう一度 Git に触らせる」必要がある。つまり.gitattributesを編集した際、既存ファイルにそのルールを適用したいならrenormalizeが必要となります。

  • すでに add 済みのファイル
  • すでに作業ディレクトリにあるファイル
git add --renormalize .
git commit -m "renormalizeの実行"
実行例
sooni@mahirospc MINGW64 /c/git/springbatch5-chunk-sample (main)
$ git add --renormalize .

sooni@mahirospc MINGW64 /c/git/springbatch5-chunk-sample (main)
$ git commit -m "Normalize line endings"
[main (root-commit) 72d4dc1] Normalize line endings
 10 files changed, 774 insertions(+)
 create mode 100644 .gitattributes
 create mode 100644 .gitignore
 create mode 100644 .mvn/wrapper/maven-wrapper.properties
 create mode 100644 mvnw
 create mode 100644 mvnw.cmd
 create mode 100644 pom.xml
 create mode 100644 src/main/java/com/oha_yo/springbatch5_chunk_sample/BatchConfig.java
 create mode 100644 src/main/java/com/oha_yo/springbatch5_chunk_sample/Springbatch5ChunkSampleApplication.java
 create mode 100644 src/main/resources/application.yml
 create mode 100644 src/test/java/com/oha_yo/springbatch5_chunk_sample/Springbatch5ChunkSampleApplicationTests.java

sooni@mahirospc MINGW64 /c/git/springbatch5-chunk-sample (main)
$
スポンサーリンク
タイトルとURLをコピーしました