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?
-
Without knowing more about what you're committing it's going to be hard to figure out the real cause here.Lasse V. Karlsen– Lasse V. Karlsen2017-12-02 17:41:55 +00:00Commented Dec 2, 2017 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– Marina Liu2017-12-04 09:02:25 +00:00Commented Dec 4, 2017 at 9: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– MikeSchem2017-12-06 20:50:07 +00:00Commented Dec 6, 2017 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– Marina Liu2017-12-08 06:42:28 +00:00Commented Dec 8, 2017 at 6:42
-
I have pushed the commits to a remote repo. I only want to remove from master.MikeSchem– MikeSchem2017-12-08 21:42:26 +00:00Commented Dec 8, 2017 at 21:42
2 Answers
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.
2 Comments
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" masterThen 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 message1The first/older commit
3f1d63aneed to used in below steps.Checkout an orphan branch from the first commit you want to keep (as the commit
3f1d63ain above example):git checkout --orphan temp 3f1d63a git commit -m 'init commit'Rebase the last one week commits based on the
tempbranch, and force pushmasterbranch: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.
4 Comments
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.