git lfs 修剪以从 lfs 中删除文件并推送到源

git lfs prune to remove files from lfs and push to origin

事情是这样的:

  1. 不小心提交了很多本不该提交的文件。
  2. 做了git reset --soft HEAD~2回到事故发生前的提交
  3. 修改gitignore 忽略文件
  4. 再次提交并推送到原点。

我假设 git 重置会逆转意外提交的所有内容,但在检查了 bitbucket 的 git lfs 文件列表后,似乎所有来自意外提交的 lfs 跟踪文件都被推送到原产地。如果我查看 bitbucket 中的源代码,这些文件不存在。

所以我尝试执行 git lfs prune,这似乎删除了一些看起来与意外提交的文件数量相当的文件,然后是 git lfs push origin master。再次检查了 bitbucket 的 git lfs 文件列表,但这些文件仍然存在并且原始文件没有任何变化。

我做错了什么?

doesn't appear to be a standard way of doing this:

The Git LFS command-line client doesn't support pruning files from the server, so how you delete them depends on your hosting provider.

Bitbucket allows you to delete LFS files using its web UI 在继续之前阅读整个链接页面):

Delete individual LFS files from your repository

It's important to understand that:

  • The delete operation described here is destructive – there's no way to recover the LFS files referenced by the deleted LFS pointer files (it's not like the git remove command!) – so you'll want to back up the LFS files first.
  • Deleting an LFS file only deletes it from the remote storage. All reference pointers stored in your Git repo will remain.
  • No branch, tag or revision will be able to reference the LFS files in future. If you attempt to check out a branch, tag or revision that includes a pointer file referencing a deleted LFS file, you'll get a download error and the check out will fail.

A repository admin can delete Git LFS files from a repo as follows:

  1. Go to the Settings page for the repo and click Git LFS to view the list of all LFS files in that repo.
  2. Delete the LFS files using the actions menu.

令人惊讶的是,从 GitHub 中删除 LFS 文件的唯一方法似乎是 delete and recreate the repository、丢失问题、星星、分叉和可能的其他数据。

在您遵循的初始步骤中,我认为您只是偶然发现了 git / git-lfs 集成并不总是完美无缝的情况之一。

reset 命令会将您的分支引用移回。它实际上不会删除不需要的提交(或相关对象);但这通常无关紧要,因为这些对象是不可访问的,所以不会用 push 发送。到目前为止一切顺利......香草 git.

但是:LFS 对象(大文件的真实内容)在您推送之前也没有被删除。 AFAIK(你的经验似乎证实了这一点)LFS 确实 而不是 尝试确定在推送到远程时是否可以访问 LFS 对象——毕竟,这似乎是一项昂贵的检查。鉴于您的 LFS 存储旨在容纳大量二进制文件,并且 LFS 旨在降低 LFS 存储中拥有大量不需要的数据的成本,成本效益通常倾向于只发送任何不需要的东西在服务器上 - 这显然是这里发生的事情。

除非您面临服务器物理存储的限制,否则这可能真的没问题。没有获取或拉取 - 没有明确告诉 LFS 向您发送所有内容,这不是正常使用的目的 - 无论如何都会导致这些文件被下载。

但也许您 运行 遇到了存储库主机的存储限制。或者,也许您只是想让它们消失;我不能说我会责怪你。在本地删除文件并推送不会导致文件从服务器中删除,这也是设计使然。 (核心 git 对象也是如此;您可以强制推送 ref 以使远程对象无法访问,但物理上 "cleaning up" 远程独立于任何本地清理。)

可在此处找到有关从 bitbucket 托管的存储库中删除 LFS 文件的信息:https://www.atlassian.com/git/tutorials/git-lfs#deleting-remote-files

对于 BitBucket 用户,我有一个解决方案,已经为我工作了几个月: https://gist.github.com/danielgindi/db0e0a897d8d920f23e155bb5d59e9c6

您基本上是在 bitbucket 存储库中打开 Chrome 并登录,然后将该段代码放入控制台。它使用您的授权去删除所有早于指定时间的 LFS 文件,这需要几秒钟。

重要说明:永远不要运行浏览器中的任何代码盲目。查看代码,确保您了解它的作用。我可以告诉你"trust me",但你不了解我