我有一个存储库,我想向其中添加大型文本数据文件。由于它们的数量和大小(在某些情况下可能高达大约 100MB),我想使用 git-lfs
跟踪这些文件。
我使用 git lfs track data.txt
添加了这样一个文件,并将默认的 -text
(指定二进制文件)更改为 text =auto
在 .gitattributes
文件中(如 git-scm's gitattributes documentation 中所述)。这给了我一个 .gitattributes
,它看起来像:
data.txt filter=lfs diff=lfs merge=lfs text=auto
为了确定,我有 refreshed the repository 。即便如此,似乎该文件仍然作为二进制对象进行跟踪,相应地,在 checkout 时不会应用行尾转换过滤器(即文件正在使用其检查的原始行结尾进行 checkout -与)。
我还尝试过使用 text=crlf
(以及变体 text eol=crlf
),得到了相同的结果。我看过许多有关使用 git-lfs 的文档和教程,但它们似乎都适合跟踪二进制文件(例如 *.bin
、 images 、 audio files 、... )
有没有办法使用 git-lfs 使文件作为大型文本文件进行跟踪(并像常规文本文件一样对行尾进行标准化)?
我目前在 Windows 7 平台上使用 git-lfs
1.5.2 和 git
for Windows 2.10.2(64 位版本), core.autocrlf = true
配置。
最佳答案
进一步阅读 git-scm's gitattributes 后经过一些修补,我能够通过定义基于 git-lfs
的自定义过滤器来实现此功能。自己的过滤器(我在 ~/.gitconfig
中找到)并利用 Jonathan Leffler的unix-to-dos conversion with sed
:
[filter "textlfs"]
clean = sed $'s/$/\\r/' %f | git-lfs clean
smudge = git-lfs smudge -- %f | sed $'s/\\r$//'
required = true
然后可以使用 .gitattributes
来跟踪 Windows 计算机上的大型文本文件。条目例如:
data.txt filter=textlfs diff=textlfs merge=textlfs
但是,这会强制存储库用户包含此自定义过滤器定义。为了方便您可以include it in a custom .gitconfig
in your repository (请注意,这需要用户手动包含 git config --local include.path ../.gitconfig
的定义)。这应该适用于 Windows 平台上的用户,但不适合具有不同行结尾的平台(例如 Linux 和 Mac)上的用户。可以使用以下内容构建更复杂的过滤器来处理不同的平台:
[filter "textlfs"]
clean = (if [ `uname -s` == "Linux" ]; then cat %f; else sed $'s/$/\\r/' %f; fi) | git-lfs clean
smudge = git-lfs smudge -- %f | (if [ `uname -s` == "Linux" ]; then cat; else sed $'s/\\r$//'; fi)
required = true
最后,请记住,除非您的大型文本文件通常在更新之间发生显着变化,或者它们太大以至于超出文件大小限制 ( such as GitHub's ),否则将这些文本文件作为标准文本文件处理可能仍然是有利的(即没有 git-lfs
)自 git can efficiently pack text files .
关于git - 如何使用 git-lfs 跟踪具有正确行结尾规范化的文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40795130/