将 Git LFS 跟踪的文件移动到常规 Git 下
Move Git LFS tracked files under regular Git
我有一个项目,我用 Git LFS 存储视频文件。现在我 运行 我的构建服务器还不支持 Git LFS,但有些问题。由于它是一项外部服务,我不能真正影响构建过程,因此我想将文件从 Git LFS 移回 "regular" Git。我设法用 git lfs untrack '<file-type>'
取消跟踪文件类型,但 git lfs ls-files
仍然给出以前添加的文件列表。
我想我可以删除文件,推送更改,然后手动重新添加它们,但这真的是推荐的做事方式吗?
Issue 641 提到了同样的问题。
I tried to stop using Git LFS, but found no way to revert my previous tracked pointer files using git lfs uninit
, git lfs untrack
, git rm
... after I move those files back it still lists as tracked by Git LFS with git lfs ls-files
, how can I opt out the whole Git LFS stuff from my repo?
答案是:
- Remove all filter.lfs.* git config entries with
git lfs uninit
.
- Clear any any attributes that use the lfs filter in
.gitattributes
by running git lfs untrack
for each file type, or deleting .gitattributes
if LFS is all you ever used it for.
在此之后,任何添加的文件将直接进入 git。
但这并不那么简单:
I later end up LFS pointer files in my working directory and have to recover all my pictures from .git/lfs
using the sha1 hash stored in those pointers manually.
2016 年 3 月更新,issue 957 illustrates a possible solution by tstephens619
:
I made the same mistake of including several small graphics formats into my git lfs
tracking list.
I was able to move this files back into git by doing the following:
Create a list of all of the files currently being tracked by git-lfs
, filter out *.gz
and *.rpm
(I want to still track those extensions with git-lfs
)
git lfs ls-files | grep -vE "\.gz|\.rpm$" | cut -d ' ' -f 3 > ~/temp/lfs-files.txt
Stop tracking the small graphics files
git lfs untrack "*.tts"
git lfs untrack "*.bfx"
git lfs untrack "*.ttf"
git lfs untrack "*.xcf"
git lfs untrack "*.pkm"
git lfs untrack "*.png"
Temporarily uninit git-lfs
git lfs uninit
# Git LFS 2.x+
git lfs uninstall
Use the file list to touch each file:
cat ~/temp/lfs-files.txt | xargs touch
git status
will now show each file as modified
Add the changes to git index (I did this via git gui
)
commit the changes and then re-init git-lfs
git commit
git lfs init
One way to do this would be:
for file in $FILES_TO_REVERT; do
git lfs untrack "$file";
git rm --cached "$file";
git add --force "$file";
done
git commit -m "..."
My preference would be not to add a command to Git LFS to the above effect, since it is possible in a number of different way with the porcelain commands provided by Git and Git LFS
我最近 运行 遇到了这个问题,资产被意外添加到一个分支上的 git-lfs,这是不应该的。我的解决方案是:
git lfs untrack '<file-type>'
git rm --cached '<file-type>'
git add '<file-type>'
git commit -m "restore '<file-type>' to git from lfs"
结果是用标准文件内容重写 git-lfs oid sha256 指针。
(编辑 2019-03):已接受的答案已更改,以便为更简单的案例提供简单的解决方案。如果您手头有更复杂的案例,另请参阅 以了解替代解决方案。
编辑:在成功使用 GIT LFS 几年后,并且对这个答案进行了多次 up/down 投票后,我认为这个警告仍然适用:GIT LFS 有很多缺陷,例如难以管理哪些文件应该在 LFS 中,在 Windows 上(不小心)将许多小文件添加到 LFS 时的性能问题,对多个远程和远程的有限支持 URL 格式,难以从 LFS 中删除文件,合并时可能 运行 遇到的各种问题,等等。 GIT LFS 是 GIT 中存在于修订树之外的外部元素。但是,我想将我原来的警告改写如下:
- 仅将通常会放入 GIT 的文件放入 GIT LFS(例如,您拥有并偶尔更改的“源文件”)
- 只将大文件放入 GIT LFS。
- 如果您需要一个系统来管理二进制依赖项,请考虑使用包管理器。
- 不要使用 Subversion 来替代 GIT LFS。更糟了。
- 准备好弄乱您的工作目录。在对 LFS 进行任何重大更改之前,请确保备份(推送)有价值的更改。
- 合并时,总是先合并
.gitattributes
。
编辑:这是我原来的回答:
很难从 GIT LFS 中删除任何内容,尽管此处提供的解决方案可能有效(经过修改),但它们需要大量工作并且可能对您的存储库产生副作用。
如果你到了这里,是时候问问自己是否要使用 GIF LFS 管理你的大文件以及是否 GIT 本身(这在管理大文件方面本来就很糟糕,因为它是分布式的版本控制系统)是个不错的选择。
如果您有很多大文件,并且您是一个组织在处理您的项目,那么像 Subversion 这样的工具可能更适合您。
我在执行 Windows 中的步骤时遇到问题。
要删除所有 git lfs 跟踪文件并恢复原始文件,我在 git bash:
中执行了以下操作
已删除。git属性
git lfs ls-files | cut -d ' ' -f 3 > lfs-files.txt
执行以下代码段:
片段:
while read file; do
git lfs untrack "$file";
git rm --cached "$file";
git add --force "$file";
done <lfs-files.txt
As of Git 2.16(2018 年 1 月 17 日发布),您可以使用 git add
的 --renormalize
标志轻松完成此操作:
git lfs untrack "<pattern>"
git add --renormalize .
git commit -m "Restore file contents that were previously in LFS"
--renormalize:
Apply the "clean" process freshly to all tracked files to
forcibly add them again to the index. This is useful after
changing core.autocrlf
configuration or the text
attribute
in order to correct files added with wrong CRLF/LF line endings.
This option implies -u
.
这里的关键部分是“所有跟踪文件”。通常,当 Git 操作更改工作树中的文件时,过滤器只有 运行。在 .gitattributes
中更改 LFS 白名单不是 Git 操作,因此在 运行 git lfs untrack
之后索引最终处于不一致状态。 运行 git add --renormalize .
告诉 Git 对存储库中的每个文件重新 运行 过滤器,这确保所有应该在 LFS 中的文件都是——并且所有不应该在 LFS 中的文件不是。
我尝试并能够成功地使用以下命令将我的回购恢复为根据他们的官方文档的常规回购:
git lfs migrate export --include="*.psd" --everything
原始官方文档的link位于https://github.com/git-lfs/git-lfs/blob/main/docs/man/git-lfs-migrate.1.ronn#migrate-local-history。
希望对遇到同样情况的人有用!
我有一个项目,我用 Git LFS 存储视频文件。现在我 运行 我的构建服务器还不支持 Git LFS,但有些问题。由于它是一项外部服务,我不能真正影响构建过程,因此我想将文件从 Git LFS 移回 "regular" Git。我设法用 git lfs untrack '<file-type>'
取消跟踪文件类型,但 git lfs ls-files
仍然给出以前添加的文件列表。
我想我可以删除文件,推送更改,然后手动重新添加它们,但这真的是推荐的做事方式吗?
Issue 641 提到了同样的问题。
I tried to stop using Git LFS, but found no way to revert my previous tracked pointer files using
git lfs uninit
,git lfs untrack
,git rm
... after I move those files back it still lists as tracked by Git LFS withgit lfs ls-files
, how can I opt out the whole Git LFS stuff from my repo?
答案是:
- Remove all filter.lfs.* git config entries with
git lfs uninit
.- Clear any any attributes that use the lfs filter in
.gitattributes
by runninggit lfs untrack
for each file type, or deleting.gitattributes
if LFS is all you ever used it for.
在此之后,任何添加的文件将直接进入 git。
但这并不那么简单:
I later end up LFS pointer files in my working directory and have to recover all my pictures from
.git/lfs
using the sha1 hash stored in those pointers manually.
2016 年 3 月更新,issue 957 illustrates a possible solution by tstephens619
:
I made the same mistake of including several small graphics formats into my
git lfs
tracking list.
I was able to move this files back into git by doing the following:
Create a list of all of the files currently being tracked by
git-lfs
, filter out*.gz
and*.rpm
(I want to still track those extensions withgit-lfs
)git lfs ls-files | grep -vE "\.gz|\.rpm$" | cut -d ' ' -f 3 > ~/temp/lfs-files.txt
Stop tracking the small graphics files
git lfs untrack "*.tts" git lfs untrack "*.bfx" git lfs untrack "*.ttf" git lfs untrack "*.xcf" git lfs untrack "*.pkm" git lfs untrack "*.png"
Temporarily uninit
git-lfs
git lfs uninit # Git LFS 2.x+ git lfs uninstall
Use the file list to touch each file:
cat ~/temp/lfs-files.txt | xargs touch
git status
will now show each file as modified
Add the changes to git index (I did this via
git gui
)commit the changes and then re-init git-lfs
git commit git lfs init
One way to do this would be:
for file in $FILES_TO_REVERT; do
git lfs untrack "$file";
git rm --cached "$file";
git add --force "$file";
done
git commit -m "..."
My preference would be not to add a command to Git LFS to the above effect, since it is possible in a number of different way with the porcelain commands provided by Git and Git LFS
我最近 运行 遇到了这个问题,资产被意外添加到一个分支上的 git-lfs,这是不应该的。我的解决方案是:
git lfs untrack '<file-type>'
git rm --cached '<file-type>'
git add '<file-type>'
git commit -m "restore '<file-type>' to git from lfs"
结果是用标准文件内容重写 git-lfs oid sha256 指针。
(编辑 2019-03):已接受的答案已更改,以便为更简单的案例提供简单的解决方案。如果您手头有更复杂的案例,另请参阅
编辑:在成功使用 GIT LFS 几年后,并且对这个答案进行了多次 up/down 投票后,我认为这个警告仍然适用:GIT LFS 有很多缺陷,例如难以管理哪些文件应该在 LFS 中,在 Windows 上(不小心)将许多小文件添加到 LFS 时的性能问题,对多个远程和远程的有限支持 URL 格式,难以从 LFS 中删除文件,合并时可能 运行 遇到的各种问题,等等。 GIT LFS 是 GIT 中存在于修订树之外的外部元素。但是,我想将我原来的警告改写如下:
- 仅将通常会放入 GIT 的文件放入 GIT LFS(例如,您拥有并偶尔更改的“源文件”)
- 只将大文件放入 GIT LFS。
- 如果您需要一个系统来管理二进制依赖项,请考虑使用包管理器。
- 不要使用 Subversion 来替代 GIT LFS。更糟了。
- 准备好弄乱您的工作目录。在对 LFS 进行任何重大更改之前,请确保备份(推送)有价值的更改。
- 合并时,总是先合并
.gitattributes
。
编辑:这是我原来的回答:
很难从 GIT LFS 中删除任何内容,尽管此处提供的解决方案可能有效(经过修改),但它们需要大量工作并且可能对您的存储库产生副作用。
如果你到了这里,是时候问问自己是否要使用 GIF LFS 管理你的大文件以及是否 GIT 本身(这在管理大文件方面本来就很糟糕,因为它是分布式的版本控制系统)是个不错的选择。
如果您有很多大文件,并且您是一个组织在处理您的项目,那么像 Subversion 这样的工具可能更适合您。
我在执行 Windows 中的步骤时遇到问题。 要删除所有 git lfs 跟踪文件并恢复原始文件,我在 git bash:
中执行了以下操作已删除。git属性
git lfs ls-files | cut -d ' ' -f 3 > lfs-files.txt
执行以下代码段:
片段:
while read file; do
git lfs untrack "$file";
git rm --cached "$file";
git add --force "$file";
done <lfs-files.txt
As of Git 2.16(2018 年 1 月 17 日发布),您可以使用 git add
的 --renormalize
标志轻松完成此操作:
git lfs untrack "<pattern>"
git add --renormalize .
git commit -m "Restore file contents that were previously in LFS"
--renormalize: Apply the "clean" process freshly to all tracked files to forcibly add them again to the index. This is useful after changing
core.autocrlf
configuration or thetext
attribute in order to correct files added with wrong CRLF/LF line endings. This option implies-u
.
这里的关键部分是“所有跟踪文件”。通常,当 Git 操作更改工作树中的文件时,过滤器只有 运行。在 .gitattributes
中更改 LFS 白名单不是 Git 操作,因此在 运行 git lfs untrack
之后索引最终处于不一致状态。 运行 git add --renormalize .
告诉 Git 对存储库中的每个文件重新 运行 过滤器,这确保所有应该在 LFS 中的文件都是——并且所有不应该在 LFS 中的文件不是。
我尝试并能够成功地使用以下命令将我的回购恢复为根据他们的官方文档的常规回购:
git lfs migrate export --include="*.psd" --everything
原始官方文档的link位于https://github.com/git-lfs/git-lfs/blob/main/docs/man/git-lfs-migrate.1.ronn#migrate-local-history。
希望对遇到同样情况的人有用!