210

githubでホストされているgitリポジトリがあります。多くのファイルは最初はWindowsで開発されたものであり、行末についてはあまり注意していませんでした。最初のコミットを実行したとき、正しい行末を強制するためのgit構成もありませんでした。結果として、githubリポジトリにCRLF行末のファイルがいくつかあります。

現在、Linuxで部分的に開発しているので、行末をクリーンアップしたいと思います。ファイルがgithubのLFで正しく保存され、作業コピーにLFが含まれていることを確認するにはどうすればよいですか?

;.gitattributesを含むファイルを設定しました。text eol=LFあれは正しいですか?それがコミットされてプッシュされたのでrm、ローカルリポジトリとgithubからの再クローンを作成して、目的の効果を得ることができますか?

4

4 に答える 4

267

リポジトリにあるファイル(純粋なソースコード、画像、実行可能ファイルなど)に関する情報がなければ、質問に答えるのは少し難しいです:)

これに加えて、WindowsまたはLinuxで作業するかどうかに関係なく、テキストファイルの.gitリポジトリにLF行末があることを確認できるため、作業ディレクトリの行末としてデフォルトでLFを使用することを検討します。 。申し訳ありませんよりも確かに安全です....

ただし、より良い代替手段があります。LinuxワークディレクトリのLF行末、WindowsワークディレクトリのCRLF行末、およびリポジトリのLF行末の利点があります。

LinuxとWindowsで部分的に作業しているので、core.eolがに設定されてnativeいることを確認core.autocrlfしてtrueください。

.gitattributes次に、ファイルの内容を次のように置き換えます

* text=auto

これにより、Gitは、コミットとチェックアウト時に自動マジックラインエンディング変換を処理できるようになります。バイナリファイルは変更されません。テキストファイルとして検出されたファイルには、行末がオンザフライで変換されます。

ただし、リポジトリのコンテンツを知っているので、Gitに手を差し伸べて、バイナリファイルからテキストファイルを検出するのを手伝ってください。

Cベースの画像処理プロジェクトで作業している場合は、.gitattributesファイルの内容を次のように置き換えます

* text=auto
*.txt text
*.c text
*.h text
*.jpg binary

これにより、拡張子がc、h、またはtxtのファイルが、リポジトリにLF行末で保存され、作業ディレクトリにネイティブ行末が含まれるようになります。Jpegファイルは変更されません。他のすべては、上記と同じ自動マジックフィルタリングの恩恵を受けます。

これらすべての内部の詳細をより深く理解するために、GithubberであるTimClemからのこの非常に優れた投稿「Mindtheendofyourline」に飛び込むことをお勧めします。

実際の例として、ファイルへの変更が示されているこのコミットを確認することもできます。.gitattributes

次のコメントを考慮して回答を更新する

Linux環境は実際にはWindowsディレクトリを共有するVirtualBoxであるため、実際にはWindowsディレクトリにCRLFは必要ありません。

理にかなっています。説明してくれてありがとう。この特定のコンテキストでは、.gitattributesファイルだけでは十分ではありません。

リポジトリに対して次のコマンドを実行します

$ git config core.eol lf
$ git config core.autocrlf input

リポジトリはLinux環境とWindows環境で共有されているため、両方の環境のローカル構成ファイルが更新されます。core.eolチェックアウト時にテキストファイルにLF行の終わりが付いていることを確認します。テキストファイル内の潜在的なCRLF(たとえば、コピー/貼り付け操作の結果)がリポジトリ内でLFに変換されることcore.autocrlfを保証します。

オプションで、次のようなものを含むファイルを作成することで、Gitテキストファイルとは何かを区別できるようにすることができます。.gitattributes

# Autodetect text files
* text=auto

# ...Unless the name matches the following
# overriding patterns

# Definitively text files 
*.txt text
*.c text
*.h text

# Ensure those won't be messed up with
*.jpg binary
*.data binary

.gitattributesファイルを作成することにした場合は、それをコミットします。

最後に、「コミットするものがない(作業ディレクトリがクリーン)」git statusと記載されていることを確認してから、次の操作を実行します

$ git checkout-index --force --all

これにより、構成の変更とファイルが考慮さ.gitattributesれ、テキストファイル内の見落とされている可能性のあるCRLFが置き換えられて、作業ディレクトリにファイルが再作成されます。

これが完了すると、作業ディレクトリ内のすべてのテキストファイルにLF行の終わりgit statusが付けられ、workdirはクリーンであると見なされます。

于 2012-04-02T14:05:26.470 に答える
198

Git 2.10(2016-09-03リリース)以降、各テキストファイルを個別に列挙する必要はありません。Git 2.10は、との動作を修正text=autoしましたeol=lfソース

.gitattributesGitリポジトリのルートにあるファイル:

* text=auto eol=lf

追加してコミットします。

その後、次の2つの手順を実行して、すべてのファイルを正規化できます。

git rm --cached -r .  # Remove every file from git's index.
git reset --hard      # Rewrite git's index to pick up all the new line endings.

出典:kenorbによる回答

于 2017-02-09T11:47:23.597 に答える
27

すべてのテキストファイルのLF行末を強制するに.gitattributesは、次の行を使用してリポジトリの最上位にファイルを作成できます(必要に応じて変更します)。

# Ensure all C and PHP files use LF.
*.c         eol=lf
*.php       eol=lf

これにより、Gitがテキストファイルと見なすすべてのファイルがLF、リポジトリ内で正規化された()行末を持つようになります(通常、core.eol構成はデフォルトでどちらを使用するかを制御します)。

新しい属性設定に基づいて、CRLFを含むテキストファイルはすべてGitで正規化する必要があります。これが自動的に行われない場合は、行末を変更した後にリポジトリを手動で更新できるため、次の手順で作業ディレクトリを再スキャンしてコミットできます(作業ディレクトリがクリーンな場合)。

$ echo "* text=auto" >> .gitattributes
$ rm .git/index     # Remove the index to force Git to
$ git reset         # re-scan the working directory
$ git status        # Show files that will be normalized
$ git add -u
$ git add .gitattributes
$ git commit -m "Introduce end-of-line normalization"

またはGitHubのドキュメントに従って:

git add . -u
git commit -m "Saving files before refreshing line endings"
git rm --cached -r . # Remove every file from Git's index.
git reset --hard # Rewrite the Git index to pick up all the new line endings.
git add . # Add all your changed files back, and prepare them for a commit.
git commit -m "Normalize all the line endings" # Commit the changes to your repository.

参照:@CharlesBaileypost

さらに、ファイルをテキストとして扱われないように除外する場合は、テキスト属性の設定を解除します。

manual.pdf      -text

または、明示的にバイナリとしてマークします。

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary

より高度なgit正規化ファイルを確認する.gitattributesには、Drupalコアで確認してください。

# Drupal git normalization
# @see https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
# @see https://www.drupal.org/node/1542048

# Normally these settings would be done with macro attributes for improved
# readability and easier maintenance. However macros can only be defined at the
# repository root directory. Drupal avoids making any assumptions about where it
# is installed.

# Define text file attributes.
# - Treat them as text.
# - Ensure no CRLF line-endings, neither on checkout nor on checkin.
# - Detect whitespace errors.
#   - Exposed by default in `git diff --color` on the CLI.
#   - Validate with `git diff --check`.
#   - Deny applying with `git apply --whitespace=error-all`.
#   - Fix automatically with `git apply --whitespace=fix`.

*.config  text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2
*.css     text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2
*.dist    text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2
*.engine  text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php
*.html    text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=html
*.inc     text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php
*.install text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php
*.js      text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2
*.json    text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2
*.lock    text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2
*.map     text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2
*.md      text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2
*.module  text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php
*.php     text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php
*.po      text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2
*.profile text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php
*.script  text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2
*.sh      text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php
*.sql     text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2
*.svg     text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2
*.theme   text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php
*.twig    text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2
*.txt     text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2
*.xml     text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2
*.yml     text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2

# Define binary file attributes.
# - Do not treat them as text.
# - Include binary diff in patches instead of "binary files differ."
*.eot     -text diff
*.exe     -text diff
*.gif     -text diff
*.gz      -text diff
*.ico     -text diff
*.jpeg    -text diff
*.jpg     -text diff
*.otf     -text diff
*.phar    -text diff
*.png     -text diff
*.svgz    -text diff
*.ttf     -text diff
*.woff    -text diff
*.woff2   -text diff

参照:

于 2016-01-15T11:37:19.230 に答える
1

Chromiumdepot_toolsを自分のMacに複製していましたが、作業コピーのすべてのファイルがCRLFで終了していました。問題を解決するこのスクリプトを見つけました。

cd <your repo>
# config the local repo to use LF
git config core.eol lf
git config core.autocrlf input

# Copy files from the index to the working tree
git checkout-index --force --all

# If above line doesn't work, delete all cached files and reset.
git rm --cached -r .
git reset --hard
于 2022-02-19T05:03:44.097 に答える