如何修改或删除 Git 中的旧提交?

How to modify or remove older commit in Git?

从过去几天开始,我一直在本地添加和提交文件。我的分支现在比 'origin/master' 提前 16 次提交。

我想将它们推送到我的 git 存储库,但是其中一个文件非常大,我的推送命令失败了。有什么方法可以从存储中删除该提交,还是我必须对头部进行硬重置。

如果您只想跳过提交,请执行 git rebase -i master 和 select drop 以跳过提交。如果您只想从中删除单个文件,select edit 并修改提交以删除该文件。

  1. 你可以用南瓜
  2. BFG

squash

# edit all the commits up to the given sha-1
git rebase -i <sha-1>


but one of the file being very large my push command fails...

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

你可以做的是使用 git 变基选项并压缩所有提交。

如果您只想删除文件,为什么不直接删除文件然后提交更改呢?

现在,在您最近的提交中该文件不存在。

您现在应该可以毫无问题地将您的分支推送到远程。