简体   繁体   中英

How to delete all the commits of a git repository before a particular commit date?

I have a git repository with more than 1000 commits. Its 2 years old. I want to keep only the last 3 months data for which I want to delete all my commits before say 1st February 2020. I tried removing old history from a git repository but it only deleted the older commits from my local machine not remotely. Also, it was deleting last n commits using HEAD~n not by date of commit.

First, deleting your history is probably a bad idea. I can't think of any reason to do it - Git handles pretty well with very large repositories, except for maybe initial clone time.

However, in the past I've had reason to do things that others couldn't think of any good reason to do - I'll leave this as your judgement call.

This blog post details a fairly simple method to do what you're asking: https://passingcuriosity.com/2017/truncating-git-history/

To summarize (don't want to plagarize), the method is to checkout a new "orphan" branch (no parent commits) specifying the hash of the oldest commit you want to keep. Commit that, then rebase the master branch onto it.

Disclaimer: I don't know what will happen to branches and tags that exist in the "keep" range. This is a potentially destructive operation, so tread lightly, and BACKUP.

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