如何删除大文件 2 git 提交回来?

how to remove a large file 2 git commits back?

基本上我想回滚我本地仓库中的两个提交。尚未将任何内容推送到远程。

所以在我的 GIT 回购中,我不小心在提交 (A) 中添加了一个巨大的 zip 文件。我意识到这一点,使用 finder 删除了 zip 文件并进行了另一次提交 (B)。

但现在我意识到,如果我将其推送到远程,它将包含整个 zip 文件...

为了让事情变得更复杂,我还对第一次提交 (A) 中的几个文件进行了一系列重要更改,我不想丢失这些更改。

如何在推送到远程之前从历史记录中删除那个 zip 文件?我可以回到这两个提交之前我的文件的状态而不丢失在其他几个文件中所做的所有更改吗?

修复它的最简单方法是使用 git reset <commit> 命令。

在你的情况下,因为你想回滚两个提交,你可以这样做:

git reset HEAD~2

来自 git help reset :

git reset [<mode>] [<commit>]

--mixed Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.

在那之后,你在你的 repos 中的文件根本不会改变,但是最近的两个提交将不再存在。

然后您可以select您想要提交哪些文件,就像您通常那样。

Link:

阅读以上内容 link 了解全部详情。

您可以使用:


摘自上述 post 这是重置

的流程


其他选项:

How to remove big files from the repository

您可以使用 git filter-branch 或 BFG。 https://rtyley.github.io/bfg-repo-cleaner/

BFG Repo-Cleaner

an alternative to git-filter-branch.

The BFG is a simpler, faster alternative to git-filter-branch for cleansing bad data out of your Git repository history:

* Removing Crazy Big Files*
* Removing Passwords, Credentials & other Private data

例子(来自官网)

In all these examples bfg is an alias for java -jar bfg.jar.

# Delete all files named 'id_rsa' or 'id_dsa' :
bfg --delete-files id_{dsa,rsa}  my-repo.git