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?
2 Answers
To remove the commits older than one week on master branch, you can use below steps:
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.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'
Rebase the last one week commits based on the
temp
branch, and force pushmaster
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.

- 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
-
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.

- 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
I have some similar problem and it has been solved. – Partha Dec 18 '17 at 07:16