2

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?

9
  • Without knowing more about what you're committing it's going to be hard to figure out the real cause here. Commented 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? Commented 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. Commented 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? Commented Dec 8, 2017 at 6:42
  • I have pushed the commits to a remote repo. I only want to remove from master. Commented Dec 8, 2017 at 21:42

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 I think the title is not very clear. Also, thanks to share your result of using those methods :). Did you see this? I think some of answers may help you. Hope it helps.
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.

4 Comments

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.
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.
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.
Did you try the commands after force push? And what's the result?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.