3

How can I delete commits older than X days?

The reason I want to do it is that the repo got quite big, filtering and gc does not help anymore, and we no more need commits older than 5 years. There are only 4 developers using this repo.

Is there any script that can automate this?

Sfisioza
  • 3,830
  • 6
  • 42
  • 57

1 Answers1

0

Two options come to my mind:

  • Create a new repository by cloning the original repo with --depth=N argument; this will create a new history keeping only the N last commits in history. All devs will then switch to this new repository.

    To find out what N must be you can use the following

      git log --format=oneline HEAD@{5 years ago} | wc -l
    

    This will however rewrite all your commits SHA1.

  • To keep your SHA1 you would use git replace, splitting the history into two, following this blog post.

Kelly Selden
  • 1,238
  • 11
  • 21
CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • 1
    Since the {X.years.ago} notation depends on the revlog, it will rarely contain data from more that 2 months ago unless it has been configured properly and you have had the clone since then. If you don't have enough revlogs that command will return the oldest revlog, which might be quite less that the 5 years intended. – Maic López Sáenz Feb 25 '13 at 18:52
  • 2
    Also, with that log (if it worked properly) you would be getting the count of the commits from 5 years ago and before, where the number that you want for `--depth` is the other way around: from now until 5 years ago. Getting a count of all commits with `git log --oneline master | wc -l` you can get a difference and the correct number. I am sad to say this is a very misleading answer. – Maic López Sáenz Feb 25 '13 at 19:49