git 从提交中删除已提交的文件

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

Remove committed files from commit

gitgithub

提问by bjornasm

I accidentaly committed some very large PDF files that were well over GitHub's size limit, so when I later pushed, I got an error, and wasn't able to push. Now, I want to remove those files from the commit, without losing them locally, so that I can later add .pdf in .gitignoreand commit and push my other changes. Does anyone know how I can do this?

我不小心提交了一些非常大的PDF文件,远远超过了GitHub的大小限制,所以我后来推送时出错,无法推送。现在,我想从提交中删除这些文件,而不会在本地丢失它们,以便我以后可以添加 .pdf.gitignore并提交并推送我的其他更改。有谁知道我怎么能做到这一点?

I do not want to undo any changes, or risk to do that.

我不想撤消任何更改,也不想冒险这样做。

回答by Jossef Harush

Backup these files first

先备份这些文件

Open terminal, cdto your git directory and

打开终端cd到您的 git 目录和

git log

You will see something like this:

你会看到这样的事情:

enter image description here

在此处输入图片说明

Then copy the commit hashbefore the addition of these large pdf files and run this command

然后在添加这些大型pdf文件之前复制提交哈希并运行此命令

git reset --soft <good commit hash>

now you can push your local changes

现在您可以推送您的本地更改

回答by nalyd88

You can try this:

你可以试试这个:

git rm *.pdf
git commit --amend

I think this should fix your commit locally and then you can push it.

我认为这应该在本地修复您的提交,然后您可以推送它。

EDIT

编辑

Copyyour PDFs to another location as the rmcommand will delete them from the git directory.

您的 PDF复制到另一个位置,因为该rm命令将从 git 目录中删除它们。