git 当我的分支领先于 master 5 次提交时,如何在提交中删除过大的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20002557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:18:42  来源:igfitidea点击:

How to remove a too large file in a commit when my branch is ahead of master by 5 commits

git

提问by Shlag Stag

I've been stuck all day on this issue, looking for an answer here :( ...

我一整天都被困在这个问题上,在这里寻找答案:( ...

Context

语境

I'm working alone on a project and I used github until now to save my work other than on my computer. Unfortunately, I added a very large file to the local repository : 300mb (which exceed Github's limit).

我在一个项目上独自工作,直到现在我都使用 github 来保存我的工作而不是在我的电脑上。不幸的是,我在本地存储库中添加了一个非常大的文件:300mb(超过了 Github 的限制)。

What I did

我做了什么

I will try to make an history of what I made :

我将尝试记录我所做的事情:

  1. I (dumbly) added everything to the index :

    git add *
    
  2. I committed changes :

    git commit -m "Blablabla"
    
  3. I tried to push to origin master

    git push origin master 
    

    It took a while, so I just CTRL+C, and repeated step 2 and 3 four times, until I realised that a file was too large to be pushed to github.

  4. I made the terrible mistake to delete my large file (I don't remember if I did a git rm or a simple rm)

  5. I followed the instructions on (https://help.github.com/articles/remove-sensitive-data)

  6. When I try to git filter branch, I get the following error : "Cannot rewrite branches: You have unstaged changes."

  1. 我(愚蠢地)将所有内容添加到索引中:

    git add *
    
  2. 我提交了更改:

    git commit -m "Blablabla"
    
  3. 我试图推到原点主人

    git push origin master 
    

    花了一段时间,所以我只是CTRL + C,并重复了四次步骤2和3,直到我意识到一个文件太大而无法推送到github。

  4. 我犯了一个可怕的错误,删除了我的大文件(我不记得我做了一个 git rm 还是一个简单的 rm)

  5. 我按照(https://help.github.com/articles/remove-sensitive-data)上的说明进行操作

  6. 当我尝试 git filter branch 时,出现以下错误:“无法重写分支:您有未暂存的更改。”

Thanks in advance !

提前致谢 !

采纳答案by Schleis

When you deleted your file, that will be a change and that is the unstaged change that git is complaining about. If you do a git status you should see the file listed as removed/deleted. To undo this change you should git checkout -- <filename>. Then the file will be back and your branch should be clean. You can also git reset --hardthis will bring your repo back to the status where you made your commit.

当您删除文件时,这将是一个更改,这就是 git 抱怨的未暂存更改。如果您执行 git status,您应该会看到该文件被列为已删除/已删除。要撤消此更改,您应该git checkout -- <filename>. 然后文件会回来,你的分支应该是干净的。您也可以git reset --hard这样将您的回购带回您提交时的状态。

I am assuming that it is the last commit that has the very large file that you want to remove. You can do a git reset HEAD~Then you can redo the commit (not adding the large file). Then you should be able to git pushwithout a problem.

我假设它是具有您要删除的非常大文件的最后一次提交。你可以做一个git reset HEAD~然后你可以重做提交(不添加大文件)。那么你应该能够git push毫无问题地进行。

Since the file is not in the last commit then you can do the final steps without a problem. You just need to get your changes either committed or removed.

由于该文件不在最后一次提交中,因此您可以毫无问题地执行最后的步骤。您只需要提交或删除您的更改。

http://git-scm.com/book/en/Git-Tools-Rewriting-History

http://git-scm.com/book/en/Git-Tools-Rewriting-History

回答by Guillaume Chevalier

A simple solution I used:

我使用的一个简单的解决方案:

  1. Do git reset HEAD^for as many commits you want to undo, it will keep your changes and your actual state of your files, just flushing the commits of them.

  2. Once the commits are undone, you can then think about how to re-commit your files in a better way, e.g.: removing/ignoring the huge files and then adding what you want and then committing again. Or use Git LFS to track those huge files.

  1. git reset HEAD^对您想要撤消的提交执行尽可能多的提交,它将保留您的更改和文件的实际状态,只需刷新它们的提交即可。

  2. 撤消提交后,您可以考虑如何以更好的方式重新提交文件,例如:删除/忽略大文件,然后添加您想要的内容,然后再次提交。或者使用 Git LFS 来跟踪那些巨大的文件。

回答by JohnWolf

The github solution is pretty neat. I did a few commits before pushing, so it's harder to undo. Githubs solution is : Removing the file added in an older commit

github 解决方案非常简洁。我在推送之前做了一些提交,所以更难撤消。Githubs 解决方案是:删除旧提交中添加的文件

If the large file was added in an earlier commit, you will need to remove it from your repository history. The quickest way to do this is with The BFG (a faster, simpler alternative to git-filter-branch):

如果大文件是在较早的提交中添加的,则需要将其从存储库历史记录中删除。最快的方法是使用 BFG(比 git-filter-branch 更快、更简单的替代品):

bfg --strip-blobs-bigger-than 50M
# Git history will be cleaned - files in your latest commit will *not* be touched

https://help.github.com/articles/working-with-large-files/

https://help.github.com/articles/working-with-large-files/

https://rtyley.github.io/bfg-repo-cleaner/

https://rtyley.github.io/bfg-repo-cleaner/

回答by Joseph Weaver

This is in reference to the BFG post above, I would comment directly, but I have no idea how to do so as a low reputation new user.

这是参考上面的BFG帖子,我会直接发表评论,但作为一个低声誉的新用户,我不知道该怎么做。

You may want to do a 'git gc' to repack first.

您可能想要先执行 'git gc' 来重新打包。

I had issues getting BFG to work until I did so, this appears to be a common issue if you've only been working in a local repo and are prepping stuff to put up on a remote for the first time.

在我这样做之前,我遇到了让 BFG 工作的问题,如果您只在本地存储库中工作并且第一次准备将东西放在遥控器上,这似乎是一个常见问题。

Relevant google hit which twigged me to it: https://github.com/rtyley/bfg-repo-cleaner/issues/65

相关的谷歌搜索让我感到震惊:https: //github.com/rtyley/bfg-repo-cleaner/issues/65

回答by JB.

It seems your only problem is having unstaged changes. You didn't give any detail as to what was actually out of sync, so it's a shot in the dark, but assuming you simple-rmd the file in step 4, you'd bring it back from the index with:

看来您唯一的问题是进行了未分阶段的更改。您没有提供任何关于实际不同步的细节,所以这是在黑暗中拍摄,但假设您rm在第 4 步中简单地删除了文件,您可以将它从索引中带回来:

git checkout large_file

If not, you're on your own. Your goal is to make sure both your index and your working tree are in the same state. This shows as git statusreporting nothing to commit, working directory clean.

如果没有,你就靠自己了。您的目标是确保您的索引和您的工作树处于相同的状态。这显示为git status报告没有提交,工作目录干净。

The nuclear option to ensure a clean tree would be git reset --hard. If you want to try that, do backup your tree+repo beforehand.

确保树木清洁的核选项将是git reset --hard。如果您想尝试,请事先备份您的 tree+repo。

Once your working copy is clean, you can proceed with your steps 5 and 6.

一旦您的工作副本干净,您就可以继续执行第 5 步和第 6 步。