如何从Git的历史中永久删除提交? [英] How to permanently delete a commit from Git's history?

查看:748
本文介绍了如何从Git的历史中永久删除提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Git的历史记录中永久删除提交?



团队中的一位开发人员意外地提交了一个200 MB文件并将其推送到我们的Git服务器。几天后它被删除,但历史在那里。我们的代码文件只有大约75 MB,并且我们有4个分支。由于200 MB的文件提交,历史记录被保留,我们的项目文件夹(特别是隐藏的.git文件夹)的大小已经膨胀到接近700 MB。如何从git永久删除两个签入(提交巨大文件,删除巨大文件),就好像它从未发生过一样?

解决方案

从结帐中删除文件



Github的实用页面如何永久删除文件(简单地说:

  $ git filter-branch --force --index-filter \ 
'git rm --cached --ignore-unmatch 200MB-filename'\
--prune-empty --tag-name-filter cat - --all
$ git push - 全部-f

这将从所有分支中删除文件。然后在本地恢复空间:

  $ rm -rf .git / refs / original / 
$ git reflog expire --expire = now --all
$ git gc --prune = now



恢复git服务器上的空间



强制执行不是删除远程服务器上的任何提交/对象。如果你不想等待git自行清理,你可以在服务器上显式运行它:

  $ ssh git server 
$ cd /my/project/repo.git
$ git gc --prune = now

比较回购前后的大小 - 确保它与您期望的大小相同。如果在将来的任何时候它恢复到更大的尺寸 - 某人已经将删除的提交回存储库(需要再次执行所有步骤)。



队友



如果有其他开发人员使用此存储库 - 他们将需要清理其结帐。否则,当他们从存储库中提取并推送他们的更改时,他们会将已删除的文件添加回,因为它仍在其本地历史记录中。有两种方法可以避免:


  1. 再次克隆

  2. 提取并重置

第一个很简单,第二个意味着两件事之一:

用户有没有本地提交



  $ git fetch 
$ git reset origin / master -hard

这将使任何本地签出完全匹配远程

用户具有本地提交



  $ git fetch 
$ git rebase -i origin / master

用户需要确保他们没有引用删除文件的任何本地提交 - 否则他们会将其添加回存储库。 / p>

用户清理



然后(可选地,因为git不会将未提交的提交推送到服务器)并且每个人都有一个一致的slimmer存储库状态:

  $ rm -rf .git / refs / original / 
$ git reflog expire --expire = n ow --all
$ git gc --prune = now


How can I permanently delete a commit from Git's history?

One of the developers on the team has accidentally committed a 200 MB file and pushed it to our Git server. It was deleted a few days later but the history is there. Our code files are only about 75 MB, and we have 4 branches. Due to the 200 MB file commit, the history is preserved and the size of our project folder (specifically the hidden .git folder) has ballooned to close to 700 MB. How do I permanently delete the two check-ins (commit of the huge file, delete of the huge file) from git as if it never happened? I'm using `TortoiseGit if that matters.

解决方案

Delete the file from a checkout

Github has a useful page how to permanently delete file(s) from a repository, in brief:

$ git filter-branch --force --index-filter \
  'git rm --cached --ignore-unmatch 200MB-filename' \
  --prune-empty --tag-name-filter cat -- --all
$ git push --all -f

That would remove the file from all branches. Then to recover the space locally:

$ rm -rf .git/refs/original/
$ git reflog expire --expire=now --all
$ git gc --prune=now

Recovering space on the git server

Force pushing does not remove any commits/objects on the remote server. If you don't want to wait for git to clean up itself, you can run it explicitly on the server:

$ ssh git server
$ cd /my/project/repo.git
$ git gc --prune=now

Compare the size of the repo before and after - ensure that it is the size you expect. If at any time in the future it reverts to the larger size - someone has pushed the deleted commits back into the repository (need to do all steps again).

Teammates

If there are other developers using this repository - they will need to clean up their checkouts. Otherwise when they pull from the repository and push their changes they will add back the deleted file as it's still in their local history. There are two ways to avoid that:

  1. Clone again
  2. fetch and reset

The first is very simple, the second means one of two things:

User has no local commits

$ git fetch
$ git reset origin/master -hard

That would make any local checkout exactly match the remote

User does have local commits

$ git fetch
$ git rebase -i origin/master

The user needs to make sure they don't have any local commits referencing the delete file - or they'll add it back to the repository.

User cleanup

Then (optionally, because git won't push unreferenced commits to the server) recover space, and everyone has a consistent slimmer repository state:

$ rm -rf .git/refs/original/
$ git reflog expire --expire=now --all
$ git gc --prune=now

这篇关于如何从Git的历史中永久删除提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆