0

I've got a git repo that I make automated commits to every day multiple times a day, but now the git repository is getting huge and taking a while to clone onto other devices. I'm wondering what the easiest way would be to slim down my repository. Is there a way to remove older commits to slim down my repository size?

MikeSchem
  • 950
  • 2
  • 16
  • 29
  • Without knowing more about *what* you're committing it's going to be hard to figure out the real cause here. – Lasse V. Karlsen Dec 02 '17 at 17:41
  • Is it ok for your team to remove the older commits (that means the older versions won't be found/tracked)? Or are you just want to remove useless files from order commits? – Marina Liu Dec 04 '17 at 09:02
  • No, I want to remove older commits. Basically, I have a git repo that has a list of suricata rules that have been changing daily. Every time a change is made a new commit is created, so there are a ton of commits, I really don't think we'd every have a reason to need a commit more than a week old. – MikeSchem Dec 06 '17 at 20:50
  • @MikeSchem Have you pushed all the commits to remote repo? And What's branches do you want to remove the old commits, only for master branch or other branches? – Marina Liu Dec 08 '17 at 06:42
  • I have pushed the commits to a remote repo. I only want to remove from master. – MikeSchem Dec 08 '17 at 21:42
  • @MikeSchem I added an answer to keep the commits in last one week, you can have a try. For other branches if you want to delete the commits older than one week, you can use the similar commands. – Marina Liu Dec 12 '17 at 08:53
  • @MikeSchem Can you clean the older commits now? – Marina Liu Dec 15 '17 at 09:10
  • If the size of the main repository isn't that important, but the cloning time is, look into [Shallow clones](https://git-scm.com/docs/git-clone), specifying `--depth X` to say "X number of commits back from the tip of the branch". Also, are you sure you want to use a git repository at all? A database might be better suited for your needs. – Lasse V. Karlsen Dec 18 '17 at 06:56
  • Visit https://stackoverflow.com/questions/2116778/reduce-git-repository-size
    I have some similar problem and it has been solved.
    – Partha Dec 18 '17 at 07:16

2 Answers2

2

To remove the commits older than one week on master branch, you can use below steps:

  1. Get the commits for the last one week which you need to keep by the command

    git log --oneline --since="one week ago" master
    

    Then you will get the commits in last one week as below (latest commit on top, older commit in bottom):

    d1fc497 (HEAD -> master, origin/master, origin/HEAD) message5
    ac89b87 message4
    8c3e839 message3
    d4ffc42 message2
    3f1d63a message1
    

    The first/older commit 3f1d63a need to used in below steps.

  2. Checkout an orphan branch from the first commit you want to keep (as the commit 3f1d63a in above example):

    git checkout --orphan temp 3f1d63a
    git commit -m 'init commit'
    
  3. Rebase the last one week commits based on the temp branch, and force push master branch:

    git rebase --onto temp 3f1d63a master
    git push -f origin master
    

Now master branch will only contains the commits for the last one week.


To remove the useless objects in your local git repo, you can use below commands:

rm -Rf .git/refs/original
rm -Rf .git/logs/
git gc
git prune --expire now

Now your local repo's size should be smaller.

Marina Liu
  • 36,876
  • 5
  • 61
  • 74
  • This didn't seem to work. I did all the steps except I didn't push to the git. When I checked the directory size it was still the same size. – MikeSchem Dec 16 '17 at 20:26
  • The reason why your local repo still in the same size is caused the useless objects are not cleaned. And I added the commands to prune your local repo size in the end of my answer, you can have a try. – Marina Liu Dec 18 '17 at 06:49
  • Did you execute `git push -f origin master` command firstly? If you are not execute the command firstly, all the useless refs and objects can not be deleted since the tracking branch `origin/master` still point to them. And after force push to remote `master` branch, you can also clone the remote repo to another directory and double check the repo's size. – Marina Liu Dec 18 '17 at 07:39
  • Did you try the commands after force push? And what's the result? – Marina Liu Dec 21 '17 at 09:16
1

git provided git gc command and git repack to improve the maintaining an old and fat repository. Also there is some un-written rules which could help to prevent the current repository grow fast(i.e using lfs for large files and so on).

Use this for more detail.

Bonje Fir
  • 787
  • 8
  • 25
  • This is a good solution for making one git repo smaller, but I want to actually remove older commits. Also, this only decreased my repo size from 470 MB to 405 – MikeSchem Dec 06 '17 at 20:48
  • @MikeSchem I think the title is not very clear. Also, thanks to share your result of using those methods :). Did you see [this](https://stackoverflow.com/questions/9683279/make-the-current-commit-the-only-initial-commit-in-a-git-repository)? I think some of answers may help you. Hope it helps. – Bonje Fir Dec 09 '17 at 06:21