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

时间:2015-01-13 11:00:40

标签: git github

我在几次提交之前意外地提交了一个大型文件,我已经将其删除并承诺,但是当我尝试推送时我得到错误:

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

有什么方法可以推送回购的当前状态并忽略我删除的那些文件?

更新

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

        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]>

2 个答案:

答案 0 :(得分:3)

在您的情况下,使用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
    
  5. 如果您已经推送了分支机构,则需要进行强制推送,因为重写了提交。否则,如果更改只是本地更改,您也可以进行正常推送。另外一个优点是,如果您在多次提交中添加了此文件,则可以从所有提交中将其从历史记录中完全删除。

答案 1 :(得分:1)

如果你没有推送这个大文件(并且由于错误我认为是这种情况),只需执行git rebase -i <some commit before the one where the large one was added>然后删除添加大文件的提交。提交将被重写,并且您将拥有一系列不具有违规权限的提交。你可以推动它。

当您执行rebase -i时,您会在编辑器中弹出这样的内容。当我说&#34;删除&#34;提交时,您只需删除列表中引用它的行(例如,假设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