简体   繁体   中英

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.

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.

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.

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

  • commit 1234 - has a couple of files related to our code base, and also merges in the whole history of the unrelated repo

  • commits before 1234 (earlier in time) are now a mix of our own commits, and the commits of the unrelated repo.

You need to do an interactive rebase:

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

Remove the commit 1034 from the list. Run git garbage collector:

git gc

Push into the upstream:

git push -f

This will wipe clean the commit 1034 from your repo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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