简体   繁体   English

如何从git存储库中完全删除/销毁提交?

[英]How can I completely remove/destroy commits from a git repository?

On the surface, this may seem the same as a million similar questions about altering history in git repos, but I believe my case is a bit different and I haven't found a solution that works for me. 从表面上看,这似乎与关于改变git repos历史的一百万个类似问题相同,但我相信我的情况有点不同,我找不到适用于我的解决方案。

I have a git repository, and at one point, one of the programmers accidentally, somehow, brought in code from a different repository, and the whole history of that (previously unrelated) repository is now part of my repositories commit log. 我有一个git存储库,有一次,其中一个程序员不小心地从不同的存储库中引入了代码,并且该(以前不相关的)存储库的整个历史记录现在是我的存储库提交日志的一部分。

Looking at the commits, it is very clear the exact commit that "brought in" this history from the previously unrelated repo. 看看这些提交,很明显是从先前无关的回购中“引入”这段历史的确切提交。

After the said commit, we did not touch any of those files. 在提交之后,我们没有触及任何这些文件。

I want to completely destroy all of these unrelated commits from my repository. 我想从我的存储库中彻底销毁所有这些无关的提交。 I don't mind if I have to do it commit by commit, but I want my repo to truly reflect our own code only. 我不介意我是否必须通过提交来提交,但我希望我的repo真正反映我们自己的代码。

If it helps, the structure is something like this: 如果它有帮助,结构是这样的:

  • commits after 1234 (more recent), do not touch any of the wrong files introduced in 1234 在1234 (更近期) 之后提交 ,请勿触摸1234中引入的任何错误文件

  • commit 1234 - has a couple of files related to our code base, and also merges in the whole history of the unrelated repo commit 1234 - 有几个与我们的代码库相关的文件,并且还合并了无关repo的整个历史记录

  • commits before 1234 (earlier in time) are now a mix of our own commits, and the commits of the unrelated repo. 在1234之前 (早期的)提交现在是我们自己的提交和无关回购的提交的混合。

You need to do an interactive rebase: 你需要做一个互动的rebase:

git rebase -i <hash-of-the-commit-1034>~

Remove the commit 1034 from the list. 从列表中删除提交1034。 Run git garbage collector: 运行git垃圾收集器:

git gc

Push into the upstream: 推入上游:

git push -f

This will wipe clean the commit 1034 from your repo. 这将清除您的仓库中的提交1034。

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

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