git - 尝试推送大文件但失败,现在我什至无法推送自述文件

标签 git github

我尝试将一些大型 csv 文件推送到 githib,但失败了,并显示一条消息告诉我文件太大。 因此,我使用 git rm --cached file.csv 删除它们,然后尝试再次推送。 然而,推送看起来像是在尝试推送像以前一样失败的同一个大文件。我怀疑这个大文件仍然以某种形式存在,我想将其从 git 中删除,以便我可以推送代码文件。

这是我尝试上述操作后在终端中看到的内容。需要一段时间才能 push ,几分钟,而考虑到没有什么可 push 的,大概应该需要几秒钟或更短的时间(至少这是我的理解,所以我很困惑为什么它看起来和行为就像它时那样)我最初尝试推送大型 csv 文件)。

git status
 3 files changed, 1814376 deletions(-)
 delete mode 100644 resources.csv
 delete mode 100644 sample_submission.csv
 delete mode 100644 train.csv
Macs-MacBook:donors_choose macuser$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .DS_Store
    .Rproj.user/
    donors_choose.Rproj
    resources.csv.zip
    sample_submission.csv.zip
    test.csv.zip
    train.csv.zip

nothing added to commit but untracked files present (use "git add" to track)
Macs-MacBook:donors_choose macuser$ git push -u origin master
Counting objects: 17, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (15/15), done.
Writing objects: 100% (17/17), 389.52 MiB | 4.59 MiB/s, done.
Total 17 (delta 2), reused 0 (delta 0)

目录中有一个自述文件,现在我想做的就是以“hello world”的方式将其添加到我的新存储库中。

终端当前显示上述代码结尾Total 17 (delta 2), reused 0 (delta 0),但是终端窗口没有响应,并且 github 上的存储库中没有显示任何内容。

编辑 几分钟没有响应后,终端窗口返回了以下消息:

To https://github.com/myusername/myrepo.git ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to 'https://github.com/myusername/myrepo.git'

最佳答案

此答案假设您最近将该大文件添加到历史记录中,并且您无法成功推送包含该大文件的任何提交。

您面临的问题是,即使删除大文件并重新提交后,该文件仍然存在于您的历史记录中。当您进行推送时,该文件将被包含在内,并且 Hook 将拒绝您的推送。

也许这里最简单的修复方法是对分支进行交互式 rebase :

git rebase -i HEAD~5

这将向您显示最近 5 个提交的列表,从最旧到最新。我假设大文件添加时间不超过 5 次提交。

pick u39mask five commits ago
pick 3jd987d four commits ago
pick 9k27dbw three commits ago
pick j1937nd two commits ago
pick wfn83e9 last commit

对于可能存在大文件的所有提交,将 pick 更改为 edit:

edit u39mask five commits ago
edit 3jd987d four commits ago
edit 9k27dbw three commits ago
edit j1937nd two commits ago
edit wfn83e9 last commit

现在保存文件,交互式 rebase 将开始。 rebase 将在您用 edit 标记的每次提交处暂停。发生这种情况时,您可以手动删除大文件,然后输入 git rebase --continue 继续下一次提交。当重新应用所有提交后, rebase 就结束了,希望您已经删除了令人不安的大文件的所有痕迹。

编辑:

如果您不熟悉将打开 rebase 提交文件的 Emacs 环境,请按 SHIFT + I 进入插入模式。然后将 pick 更改为您想要的任何内容,例如编辑。完成编辑后,按 ESC,然后按 :wq 保存并关闭。然后,rebase 将开始。

关于git - 尝试推送大文件但失败,现在我什至无法推送自述文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49145104/

相关文章:

python - 无法通过gitpython克隆存储库

github - 无法在 github 搜索中排除多个作者

git - 将 ~/.gitconfig 中的 github.token 别名为 shell 命令

github - 获取上次从 Github 存储库更新文件的时间

xcode - 如何使用 GitHub Actions 安装私有(private) Pod?

适用于 Windows 的 Git : can't run any commands after command git config --list

git - 推送后更改 git 提交消息(假设没有人从远程 pull )

git - 在 VS2013 中查看两个 git 修订版之间的差异

Git pull 与 pull 请求

git - 等待 "git clone"在 bash 中完成