2

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.

  • 1
    After rewriting the local history you must be using `git push -f` to replace commits in the remote repository. **NOTE!** You have rewritten the existing commits, anyone having a clone of your repository must re-clone the new repository after you have removed old commits. If they start pushing and pulling they risk reintroducing all those old commits into your repository again. – Lasse V. Karlsen May 18 '20 at 11:20
  • 1
    And you will have to figure out which commit you want to be the first one you want to keep, in other words, *you* have to do the work of figuring out which commit it is you want to keep based on dates, by looking at the logs. – Lasse V. Karlsen May 18 '20 at 11:20
  • 2
    This is too broad, and no one can answer. It completely depends on your code. I am sure you use case can be solved with some different strategy rather than deleting commit history. Keeping the commit history is of no harm. – Rakmo May 18 '20 at 11:43
  • 1
    Why do you want to delete the old commits? 1000 commits is not a bug repository (and 2 years is not old). My repo has 62,000 commits and counting. – thelr May 18 '20 at 12:09

1 Answers1

0

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.

thelr
  • 1,134
  • 11
  • 30