简体   繁体   English

推送后删除git远程上的二进制文件

[英]remove binary file on git remote after push

How can I wipe away a file in a remote git repository (created via git init --bare , after I have pushed from my local machine (which was created via git clone )? I have root access to my server. I tried ssh-ing into the machine and doing git reset --hard HEAD~1 from the directory where the bare repo was made, but I get an error message stating that I am not in a work tree.如何擦除远程 git 存储库中的文件(通过git init --bare创建,在我从本地机器(通过git clone创建)推送后)?我对我的服务器有 root 访问权限。我试过 ssh-进入机器并从制作裸git reset --hard HEAD~1的目录中执行git reset --hard HEAD~1 ,但我收到一条错误消息,指出我不在工作树中。

Here's my situation.这是我的情况。 I have a cross-platform project for which I keep backups on a git server on my home network.我有一个跨平台项目,我在我的家庭网络上的 git 服务器上保留了备份。 I am storing IDE files along with my source code, so that pulling the entire repository on a "fresh" machine that already has the IDE installed has everything required to open the solution or workspace, build, and run.我将 IDE 文件与我的源代码一起存储,以便将整个存储库拉到已经安装了 IDE 的“新”机器上,其中包含打开解决方案或工作区、构建和运行所需的一切。 Very late last night when my attention span was all but gone, I checked in a huge commit that contains large binary files (~35MB) that Visual Studio generates during runtime ( SolutionName.VC.db , SolutionName.VC.VC.opendb , among others).昨晚很晚,当我的注意力几乎消失时,我检查了一个巨大的提交,其中包含 Visual Studio 在运行时生成的大型二进制文件(~35MB)( SolutionName.VC.dbSolutionName.VC.VC.opendb ,其中其他)。 I discovered that these files slow down a git clone considerably, so I want to remove them completely from my repository.我发现这些文件会大大减慢git clone速度,所以我想从我的存储库中完全删除它们。

I am willing to checkout an old copy and re-commit/re-push the changes.我愿意签出旧副本并重新提交/重新推送更改。 Also, as a preventative measure, I have already added the unwanted files to my .gitignore .此外,作为预防措施,我已经将不需要的文件添加到我的.gitignore

You're on the right track with git reset --hard HEAD~1 , but you should be doing that in your clone, not on the server.您使用git reset --hard HEAD~1在正确的轨道上,但您应该在您的克隆中这样做,而不是在服务器上。 And after you do this, you need to push to the server, with the -f option to force rewriting the history :执行此操作后,您需要推送到服务器,并使用-f选项强制重写历史记录

git reset --hard HEAD~1
git push origin master -f

After this, update the .gitignore and commit it, then add the other changes without the Visual Studio files accidentally committed earlier.在此之后,更新.gitignore并提交它,然后添加其他更改,而没有之前意外提交的 Visual Studio 文件。 Finally commit and push again, your relevant changes should be there again, and cloning should be fast again.最后再次提交并推送,您的相关更改应该再次出现,并且克隆应该再次快速。

git rm filename 
git commit 
git push 

should do the job.应该做的工作。 This deletes the file locally as well though so don't use it for on files you just don't want to have online yet.这也会在本地删除文件,因此不要将它用于您只是不想在线的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM