删除 github 上意外提交的大量文件

Remove accidentally committed massive file on github

我在几次提交前不小心提交了一个大文件,我删除了它并提交了它,但是当我尝试推送时我得到 error :

'remote: error: GH001: Large files detected.'

有什么办法可以只推送存储库的当前状态而忽略我删除的那些文件吗?

更新

当我 运行 git 变基时,我得到了这个..

        C:\Data\unity\GameX4 [patching]> git rebase 4a877be9acb7dbabb46b9aec367d68b2fec7c884
    First, rewinding head to replay your work on top of it...
    Applying: smaller particles again
    Applying: sdgs
    Using index info to reconstruct a base tree...
    M       Assets/01_GRAPHICS/03_UNITY_MATERIALS/metaballmat.mat
    Falling back to patching base and 3-way merge...
    warning: Cannot merge binary files: Assets/01_GRAPHICS/03_UNITY_MATERIALS/metaballmat.mat (HEAD vs. sdgs)
    Auto-merging Assets/01_GRAPHICS/03_UNITY_MATERIALS/metaballmat.mat
    CONFLICT (content): Merge conflict in Assets/01_GRAPHICS/03_UNITY_MATERIALS/metaballmat.mat
    Failed to merge in the changes.
    Patch failed at 0002 sdgs
    The copy of the patch that failed is found in:
       c:/Data/unity/GameX4/.git/rebase-apply/patch

    When you have resolved this problem, run "git rebase --continue".
    If you prefer to skip this patch, run "git rebase --skip" instead.
    To check out the original branch and stop rebasing, run "git rebase --abort".

    C:\Data\unity\GameX4 [(1697860...)|REBASE +0 ~3 -0 !1 | +0 ~0 -0 !1]>

如果您还没有推送这个大文件(我推测是由于错误导致的),只需执行 git rebase -i <some commit before the one where the large one was added> 然后删除您添加大文件的提交。提交将被重写,您将拥有一串没有违规提交的提交。你将能够推动这个。

当你执行 rebase -i 时,你会在你的编辑器中看到类似这样的内容。当我说 "delete" 提交时,您只需删除列表中引用它的行(例如,假设 faed141 是您添加大文件的提交,只需删除引用的行在下面的列表中)。然后保存并退出你的编辑器。 Git 将重写删除该提交的树,你就可以开始了。

pick 53542eb Changes a
pick a30f028 Adds b
pick faed141 Adds c
pick 5446c0f Adds d

# Rebase fbd339b..5446c0f onto fbd339b
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

在你的情况下,使用 git filter-branch 从所有以前的提交中删除有问题的文件

  1. 使用命令删除file_name_to_remove

    git filter-branch --force --index-filter \
      'git rm -r --cached --ignore-unmatch file_name_to_remove' \
      --prune-empty --tag-name-filter cat -- --all
    
  2. 过滤分支完成后,确认没有意外文件丢失。

  3. 现在添加一个.gitignore规则

    echo "file_name_to_remove" >> .gitignore
    git add .gitignore && commit -m "removing filename"
    
  4. 现在推送

    git push -f origin branch
    

如果你已经推送了你的分支,你将需要强制推送,因为提交被重写了。否则,如果更改只是本地的,您也可以进行正常的推送。额外的好处是,如果您在多次提交中添加了此文件,则可以将其从所有提交的历史记录中完全删除。