git 由于我已经删除的大文件,无法推送到 GitHub

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19573031/
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:10:44  来源:igfitidea点击:

Can't push to GitHub because of large file which I already deleted

gitgithubgit-push

提问by Kevin W.

Currently I have

目前我有

  1. Empty GitHub repo
  2. SSH server repo (main)
  3. Local Repo
  1. 空的 GitHub 存储库
  2. SSH 服务器存储库(主要)
  3. 本地回购

SSH server repo was the most up-to-date repo (production site) so I did a Git clone from there to local. I then tried to do a git pushto GitHub.

SSH 服务器存储库是最新的存储库(生产站点),所以我从那里复制了一个 Git 到本地。然后我尝试git push对 GitHub做一个。

Everything went OK but then it said something about filename.gz being too large for GitHub. I didn't need this file so I ran several Git commands to get rid of it from Git cache then pushed back to SSH server.

一切顺利,但后来它说 filename.gz 对于 GitHub 来说太大了。我不需要这个文件,所以我运行了几个 Git 命令来从 Git 缓存中删除它,然后推送回 SSH 服务器。

I don't see the large file locally but it's still on SSH server even though git diffreturns nothing and git push returns "Everything is up-to-date" - And even though the file is not visible in local repo when I try to push to GitHub I still get error about it

我在本地看不到大文件,但它仍然在 SSH 服务器上,即使git diff什么都不返回并且 git push 返回“一切都是最新的” - 即使当我尝试推送到本地存储库时该文件不可见GitHub 我仍然收到关于它的错误

remote: error: File fpss.tar.gz is 135.17 MB; this exceeds GitHub's file size limit of 100 MB

远程:错误:文件 fpss.tar.gz 为 135.17 MB;这超出了 GitHub 的 100 MB 文件大小限制

I followed steps under "fixing the problem" listed on GitHub helpso shouldn't that have been enough?

我遵循了 GitHub 帮助中列出的“修复问题”下的步骤,所以这还不够吗?

How is the file still in the ether when it's not local or listed in git status/diff/push?

当文件不在本地或不在 git status/diff/push 中时,它如何仍在以太坊中?

回答by MacGyver

You can use

您可以使用

git filter-branch --index-filter 'git rm -r --cached --ignore-unmatch <file/dir>' HEAD

This will delete everything in the history of that file. The problem is that the file is present in the history.

这将删除该文件历史记录中的所有内容。问题是该文件存在于历史记录中。

This command changes the hashes of your commits which can be a real problem, especially on shared repositories. It should not be performed without understanding the consequences.

此命令会更改提交的哈希值,这可能是一个真正的问题,尤其是在共享存储库上。在不了解后果的情况下,不应执行此操作。

回答by But I'm Not A Wrapper Class

I found squashingmore useful than filter-branch. I did the following:

我发现挤压filter-branch. 我做了以下事情:

  1. Locally delete large files.
  2. Commit the local deletes.
  3. Soft reset back X number of commits (for me it was 3): git reset --soft HEAD~3.
  4. Then recommit all the changes together (AKA squash) git commit -m "New message for the combined commit"
  5. Push squashed commit.
  1. 本地删除大文件。
  2. 提交本地删除。
  3. 提交的软复位回到X号(对我来说是3) git reset --soft HEAD~3
  4. 然后一起重新提交所有更改(又名壁球) git commit -m "New message for the combined commit"
  5. 推送压扁的提交。

Special case(from user @lituo): If above doesn't work, then you may have this case. Commit 1 included the large file and Commit 1's push failed due to large file error. Commit 2 removed the large file by git rm --cached [file_name]but Commit 2's push still failed. You can follow the same steps above but instead of using HEAD~3, use HEAD~2.

特殊情况(来自用户@lituo):如果以上不起作用,那么您可能有这种情况。提交 1 包含大文件,由于大文件错误,提交 1 的推送失败。提交 2 删除了大文件,git rm --cached [file_name]但提交 2 的推送仍然失败。您可以按照上述相同的步骤操作,但不要使用HEAD~3,而是使用HEAD~2.

回答by Shreya

Here's something I found super helpful if you've already been messing around with your repo before you asked for help. First type:

如果您在寻求帮助之前已经弄乱了您的存储库,那么我发现这里有一些非常有用的东西。第一种:

git status

After this, you should see something along the lines of

在此之后,您应该会看到类似以下内容的内容

On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

The important part is the "2 commits"! From here, go ahead and type in:

重要的部分是“2次提交”!从这里,继续输入:

git reset HEAD~<HOWEVER MANY COMMITS YOU WERE BEHIND>

So, for the example above, one would type:

因此,对于上面的示例,可以键入:

git reset HEAD~2

After you typed that, your "git status" should say:

输入后,您的“git status”应该会显示:

On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

From there, you can delete the large file (assuming you haven't already done so), and you should be able to re-commit everything without losing your work.
I know this isn't a super fancy reply, but I hope it helps!

从那里,您可以删除大文件(假设您还没有这样做),并且您应该能够重新提交所有内容而不会丢失您的工作。
我知道这不是一个超级花哨的回复,但我希望它有所帮助!

回答by BlueMoon93

If the file was added with your most recent commit, and you have not pushed to the remote repository, you can delete the file and amend the commit, Taken from here:

如果文件是用你最近的提交添加的,并且你没有推送到远程存储库,你可以删除文件并修改提交,取自这里

git rm --cached giant_file
    # Stage "giant_file" for removal with "git rm"
    # Leave it on disk with "--cached". if you want to remove it from disk
    # then ignore the "--cached" parameter
git commit --amend -CHEAD
    # Commit the current tree without the giant file using "git commit"
    # Amend the previous commit with your change "--amend" 
    # (simply making a new commit won't work, as you need
    # to remove the file from the unpushed history as well)
    # Use the log/authorship/timestamp of the last commit (the one we are
    # amending) with "-CHEAD", equivalent to --reuse-message=HEAD
git push
    # Push our rewritten, smaller commit with "git push"

回答by Andre Odendaal

I had a similar issue and used the step aboveto remove the file. It worked perfectly.

我遇到了类似的问题,并使用上述步骤删除了文件。它工作得很好。

I then got an error on a second file that I needed to remove: remote: error: File <path/filename> is 109.99 MB; this exceeds GitHub's file size limit of 100.00 MB

然后我在需要删除的第二个文件上遇到错误: remote: error: File <path/filename> is 109.99 MB; this exceeds GitHub's file size limit of 100.00 MB

I tried the same step, got an error: "A previous backup already exists in <path/filename>"

我尝试了相同的步骤,但出现错误: "A previous backup already exists in <path/filename>"

From research on this websiteI used the command: git filter-branch --force --index-filter "git rm --cached --ignore-unmatch <path/filename>" --prune-empty --tag-name-filter cat -- --all

通过对本网站的研究,我使用了以下命令:git filter-branch --force --index-filter "git rm --cached --ignore-unmatch <path/filename>" --prune-empty --tag-name-filter cat -- --all

Worked great, and the large files were removed.

效果很好,并且删除了大文件。

Unbelievably, the push still failed with another error: error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 104 fatal: The remote end hung up unexpectedly

令人难以置信的是,推送仍然失败,并出现另一个错误: error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 104 fatal: The remote end hung up unexpectedly

This I fixed by directly modifying the .git config file - postBuffer = 999999999

我通过直接修改 .git 配置文件来解决这个问题 - postBuffer = 999999999

After that the push went through!

之后,推动通过了!

回答by Roberto Tyley

Why is GitHub rejecting my repo, even after I deleted the big file?

为什么 GitHub 拒绝我的 repo,即使我删除了大文件?

Git stores the full history of your project, so even if you 'delete' a file from your project, the Git repo still has a copy of the file in it's history, and if you try to push to another repository (like one hosted at GitHub) then Git requiresthe remote repo has the same history that your local repo does (ie the same big files in it's history).

Git 存储了项目的完整历史记录,因此即使您从项目中“删除”了一个文件,Git 存储库的历史记录中仍然有该文件的副本,并且如果您尝试推送到另一个存储库(例如托管在GitHub) 然后 Git要求远程存储库具有与本地存储库相同的历史记录(即历史记录中的相同大文件)。

How can I can I get GitHub to accept my repo?

我怎样才能让 GitHub 接受我的回购?

You need to clean the Git history of your project locally, removing the unwanted big files from all of history, and then use only the 'cleaned' history going forward. The Git commit ids of the affected commits will change.

您需要在本地清理项目的 Git 历史记录,从所有历史记录中删除不需要的大文件,然后仅使用“已清理”的历史记录。受影响提交的 Git 提交 ID 将发生变化。

How do I clean big files out of my Git repo?

如何清除 Git 存储库中的大文件?

The best tool for cleaning unwanted big files out of Git history is the BFG Repo-Cleaner- it's a simpler, faster alternative to git-filter-branchspecifically designed for removing unwanted files from Git history.

从 Git 历史记录中清除不需要的大文件的最佳工具是BFG Repo-Cleaner- 它是一种更简单、更快的替代品,git-filter-branch专门用于从 Git 历史记录中删除不需要的文件。

Carefully follow the usage instructions, the core part is just this:

仔细按照使用说明,核心部分就是这样:

$ java -jar bfg.jar --strip-blobs-bigger-than 100M my-repo.git

Any files over 100MB in size (that aren't in your latestcommit) will be removed from your Git repository's history. You can then use git gcto clean away the dead data:

任何超过 100MB 的文件(不在您的最新提交中)都将从您的 Git 存储库的历史记录中删除。然后您可以使用git gc清除死数据:

$ git gc --prune=now --aggressive

The BFG is typically at least 10-50xfaster than running git-filter-branch, and generally a lot easier to use.

BFG 通常至少比 running 快10-50git-filter-branch,并且通常更易于使用。

Full disclosure: I'm the author of the BFG Repo-Cleaner.

完全披露:我是 BFG Repo-Cleaner 的作者。

回答by William Hu

I got the same problem and none of the answers work for me. I solved by the following steps:

我遇到了同样的问题,没有一个答案对我有用。我通过以下步骤解决了:

1. Find which commit(s) contains the large file

1. 查找包含大文件的提交

git log --all -- 'large_file`

The bottom commit is the oldestcommit in the result list.

底部提交是结果列表中最早的提交。

2. Find the one just before the oldest.

2. 找到最老的之前的那个。

git log

Suppose you got:

假设你有:

commit 3f7dd04a6e6dbdf1fff92df1f6344a06119d5d32

3. Git rebase

3. git rebase

git rebase -i 3f7dd04a6e6dbdf1fff92df1f6344a06119d5d32

Tips:

小贴士

  1. List item
  2. I just choose dropfor the commits contains the large file.
  3. You may meet conflicts during rebase fix them and use git rebase --continueto continue until you finish it.
  4. If anything went wrong during rebase use git rebase --abortto cancel it.
  1. 项目清单
  2. 我只是选择drop包含大文件的提交。
  3. 您可能会在 rebase 期间遇到冲突修复它们并使用git rebase --continue它继续直到您完成它。
  4. 如果在 rebase 期间出现任何问题,请使用git rebase --abort取消它。

回答by Shuaibin Chang

I have tried all above methods but none of them work for me.

我已经尝试了上述所有方法,但没有一个对我有用。

Then I came up with my own solution.

然后我想出了我自己的解决方案。

  1. First of all, you need a clean, up-to-date local repo. Delete all the fworing large files.

  2. Now create a new folder OUTSIDE of your repo folder and use "Git create repository here" to make it a new Git repository, let's call it new_local_repo. This is it! All above methods said you have to clean the history..., well, I'm sick of that, let's create a new repo which has no history at all!

  3. Copy the files from your old, fwored up local repo to the new, beautiful repo. Note that the green logo on the folder icon will disappear, this is promising because this is a new repo!

  4. Commit to the local branch and then push to remote new branch. Let's call it new_remote_branch. If you don't know how to push from a new local repo, Google it.

  5. Congrats! You have pushed your clean, up-to-date code to GitHub. If you don't need the remote master branch anymore, you can make your new_remote_branch as new master branch. If you don't know how to do it, Google it.

  6. Last step, it's time to delete the fwored up old local repo. In the future you only use the new_local_repo.

  1. 首先,您需要一个干净的、最新的本地存储库。删除所有他妈的大文件。

  2. 现在在您的 repo 文件夹之外创建一个新文件夹,并使用“Git create repository here”将其设为新的 Git 存储库,我们将其命名为 new_local_repo。就是这个!以上所有方法都说你必须清理历史......好吧,我厌倦了,让我们创建一个完全没有历史的新仓库!

  3. 将文件从旧的、搞砸的本地仓库复制到新的、漂亮的仓库。请注意,文件夹图标上的绿色徽标将消失,这是有希望的,因为这是一个新的存储库!

  4. 提交到本地分支,然后推送到远程新分支。我们称之为 new_remote_branch。如果你不知道如何从一个新的本地仓库推送,谷歌一下。

  5. 恭喜!您已将干净、最新的代码推送到 GitHub。如果您不再需要远程 master 分支,则可以将 new_remote_branch 设为新的 master 分支。如果你不知道怎么做,谷歌一下。

  6. 最后一步,是时候删除该死的旧本地存储库了。将来您只能使用 new_local_repo。

回答by Kiprono Elijah Koech

The solution to keep the large files/folders within the working folder

将大文件/文件夹保留在工作文件夹中的解决方案

This is the line that worked to solve the problem asked here (from answer 1):

这是用于解决此处提出的问题的行(来自答案 1):

git filter-branch --index-filter 'git rm -r --cached --ignore-unmatch <file/dir>' HEAD

git filter-branch --index-filter 'git rm -r --cached --ignore-unmatch <file/dir>' HEAD

This command also delete the file/dir if the file/dir is within the working tree.

如果文件/目录在工作树中,此命令还会删除文件/目录。

If you want to keep the file/folder within the working tree I propose taking the following steps.

如果您想将文件/文件夹保留在工作树中,我建议采取以下步骤。

  1. After that error run git reset HEAD^
  2. Add the file/folder in question into ``.gitignore``` file.

  3. Proceed as usual git add .which might capture other files/folders but must capture .gitignorefile. Next is git commit -m"message"and finally git push origin <branch_name>

  1. 在那个错误运行之后 git reset HEAD^
  2. 将有问题的文件/文件夹添加到 ``.gitignore`` 文件中。

  3. 照常进行git add .,这可能会捕获其他文件/文件夹,但必须捕获.gitignore文件。接下来是git commit -m"message"最后git push origin <branch_name>