2

I have a git repository with about 1 year of development history, and it is already 37GB. How can I minimize the size in a way where I will delete the old history? That is, I only need the history that covers the last 2 months, others can be removed.

Jeroen
  • 60,696
  • 40
  • 206
  • 339
rkevx21
  • 2,441
  • 5
  • 19
  • 40
  • How large is the `.git` directory? – Himanshu Mishra Jul 15 '15 at 07:02
  • @HimanshuMishra Hello Sir. The .git directory is about 35G. – rkevx21 Jul 15 '15 at 07:04
  • 2
    37G?! I think you have added the executables, movies, photos... IMHO, it is a bad practice, you shall add just the files you need (for test, maybe one or 2 multimedia files) – sop Jul 15 '15 at 07:07
  • Yes, I think that too. It's very rare that your piece of code comprises of 35GB. 1 GB is rare. Have you accidentally commited some large file which is not required? You can also sort the files with size and see are there unnecessary files on the top? – Himanshu Mishra Jul 15 '15 at 07:10
  • @sop yes, it is 37G , since my repository is database tables – rkevx21 Jul 15 '15 at 07:13
  • @HimanshuMishra yes, it is 37G , since my repository is database tables – rkevx21 Jul 15 '15 at 07:14
  • 1
    Does [this](http://stackoverflow.com/questions/2100907/how-to-remove-delete-a-large-file-from-commit-history-in-git-repository) help? – Himanshu Mishra Jul 15 '15 at 07:17

1 Answers1

1

The three main options are:

On the last point, see "How do I remove the old history from a git repository?", using a script like this one (with the SHA1 of the commit from 2 months ago, as a parameter)

#!/bin/bash
git checkout --orphan temp $1
git commit -m "Truncated history"
git rebase --onto temp $1 master
git branch -D temp

# The following 2 commands are optional - they keep your git repo in good shape.
git prune --progress # delete all the objects w/o references
git gc --aggressive # aggressively collect garbage; may take a lot of time on large repos

Since it rewrites the history, you will need a git push --force.
Since the OP is pushing to git.assembla.com (see discussion), this issue clearly states

You need to enable the --force option.
This is done from the Git repository Settings tab. You need to be an 'Owner' on the space to see the settings tab.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I save the shell script as git-remove-history.sh, how can I execute it sir ? bash git-remove-history.sh ? – rkevx21 Jul 15 '15 at 07:40
  • @rkevx21 what version of Git are you using. What OS (Windows, Linux, Mac) are you using (at which version) – VonC Jul 15 '15 at 07:41
  • @rkevx21 What version of Linux (type `uname -a`) and with what version of Git? (type `git --version`) – VonC Jul 15 '15 at 07:44
  • Linux rkevx21 3.13.0-53-generic #89-Ubuntu SMP Wed May 20 10:34:39 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux – rkevx21 Jul 15 '15 at 07:45
  • 1
    @rkevx21 Ok. A simple `./git-remove-history.sh ` should be enoough (make sure you have your repo backed up first, or simply available in a Git repo hosting server somewhere before doing that). Replace `` with the SHA1 of the commit from which you want to start your history (the one commit which is two months old) – VonC Jul 15 '15 at 07:47
  • ah.. so it means that the is the commit id ? – rkevx21 Jul 15 '15 at 08:01
  • @rkevx21 the commit id of the commit from which you want to keep the history. – VonC Jul 15 '15 at 08:02
  • I have this message WARNING: Ref 'refs/heads/master' is unchanged – rkevx21 Jul 15 '15 at 08:22
  • @rkevx21 the master should reflect the new history (a git log should only show the last commits) – VonC Jul 15 '15 at 08:24
  • sir , when I run the script, it works, but my problem now is how can I push it to the repository, since it will give you a message to pull first before I can push. – rkevx21 Jul 16 '15 at 03:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83378/discussion-between-rkevx21-and-vonc). – rkevx21 Jul 16 '15 at 03:45
  • @rke It rewrites the history of the repo, so you need a Git mush --force. – VonC Jul 16 '15 at 03:46