从历史记录中删除大文件/完全从本地删除历史记录,然后将其推送到远程仓库。

3

我是一个完全不懂git的新手,感到非常困惑。我相信我错过了一些基本的东西。

问题如下:

  1. I committed and pushed large files (~2Gb) to a repository. Then I removed them - committed - pushed, but they are still in the history. I need to shrink the repository somehow.
  2. First, I followed how to shrink a git repository. I could only shrink the local version, but when I tried to push, it said that I have to pull first. Pulling, however, would bring the big history files back.
  3. Similar to revert multiple git commits, I removed completely the last two commits by doing twice

    git reset --hard HEAD^
    git push -f origin master
    
  4. Now, I can not see the last two commits in git log, but the repository/history is still big.

  5. As the files are not in the history anymore, I can not remove deleted files from git history, as git does not see them.

假设我是这个代码库的唯一使用者,历史记录没有价值,我只是需要缩小代码库的大小。但是,我只能在本地进行更改,然后将它们推送到代码库中,因此,完全删除代码库不起作用。

    rm -rf .git

删除git后,我将无法进行推送操作。

删除git对我有多大的影响?我该如何实现我的总体目标?如果有任何建议,我将不胜感激。提前致谢。


当你写下“然后我删除它们”时,你是指使用git rm还是普通的rm命令? - jub0bs
这些文件是使用普通的rm命令删除的,但它们仍然存储在历史文件中。 - Anastasia
嘿Jubobs,实际上,我还没有解决这个问题。我在下面的评论中描述了我仍然存在的问题。我会感激任何帮助。 - Anastasia
2个回答

1

@Jubobs总结一下,为了使上述解决方案起作用,我不得不更改远程存储库的一些设置。特别是,我不得不允许以下操作

git push -f origin master

首先通过ssh连接远程仓库,然后在那里进行设置。

git config receive.denyNonFastforwards true

此外,为了清理远程存储库中过时文件占用的空间,请运行以下命令:
git gc --aggressive --prune=all

一个自然的问题出现了:难道不是更容易(1)ssh到远程仓库,(2)删除.git文件夹,(3)运行git init来初始化一个新的仓库吗?好吧,这也是一个解决方案。

0

只有当(请确保您同意以下每个要点)

  • 历史记录没有价值,
  • 除了您之外,没有其他人依赖于远程存储库,
  • 您希望在工作树中保留所有文件(但不一定在存储库中),这些文件目前都在您的工作树中(而不是被埋在某个提交中),
  • 您想使用全新的本地存储库重新开始,然后将该存储库推送到远程,

请执行以下操作。删除项目目录中的.git文件夹,然后重新初始化Git。

rm -rf .git
git init

将您的远程仓库添加到存储库配置中:

git remote add origin <url-of-remote-in-question>

拍摄快照:

git add <at-your-discretion>
git commit -m "initial commit"

然后强制推送你全新的master分支到远程:

git push -f origin master

编辑:如果最后一个强制推送命令失败了,那么这意味着你的远程仓库配置拒绝非快进合并。请参见Source Forge repo gives "denying non-fast-forward refs/heads/master" error


感谢您的详细回复。实际上,这并没有解决我的问题。事实上,在执行最后一个命令(git push)时,我收到了以下错误: - Anastasia
远程:错误:拒绝非快速转发 refs/heads/master(您应该先进行拉取) 到<问题中的远程URL> ![拒绝的远程] master -> master(非快速转发) 错误:无法将某些 refs 推送到'<问题中的远程URL>' - Anastasia
当我之后再次尝试拉取时,它又把我所有的大文件都拉回来了。有什么建议吗? - Anastasia
如果您是远程仓库的唯一所有者并且不需要远程的 master 分支,请尝试按照上面概述的步骤进行操作,但在进行最终推送之前,执行 git push -f origin :master(注意 :);这将删除驻留在远程仓库中的 master 分支。然后,如果一切正常,请尝试将本地的 master 分支推送到远程:git push origin master。完成后,请在此报告。 - jub0bs
抱歉回复晚了。我联系了我的系统管理员(该仓库是公司的),当我找到问题时,我会回到这篇帖子(尽管可能需要几周时间)。 - Anastasia
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接