Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compact repo by removing old commits

Tags:

git

Is there a way to reduce repo size by removing local copy of data about old commits?

Similar to how git clone --depth 5 produces a small local clone with only recent commit data.

The repo contains gigabytes of game assets which were overwritten multiple times in past.

Edit: I don't want to just purge the past history; I want the history to remain consistent with the remote, but I don't want old commits to be locally stored and available.

like image 808
Eugene Pankov Avatar asked May 31 '13 09:05

Eugene Pankov


2 Answers

Unfortunately, it is NOT possible to keep only most recent commits in history and have fully usable repository. Shallow copy is NOT fully usable because it cannot be committed into.

In other words, because of the way git keeps data in object store, you don't have many options other than rewriting history from scratch.

Probably easiest approach is to make a copy of all current game asset files somewhere outside of git repository, then use git filter-branch as described here to remove your assets from history, like it never existed. Finally, copy assets back into their rightful place and commit them once again. This will rewrite your history in a way that game assets only appear in most recent commit.

However, it will still not reclaim disk space though until you do:

git reflog expire --expire=now --all
git gc --prune=now
like image 91
mvp Avatar answered Nov 09 '22 18:11

mvp


git gc --prune=<date> should do what you want. Take a look into documentation: https://www.kernel.org/pub/software/scm/git/docs/git-gc.html

Update1: I suggest you to read this blog post about git gc as well: http://gitfu.wordpress.com/2008/04/02/git-gc-cleaning-up-after-yourself/

Update2: @mvp is right, after a deep research git gc will only remove untracked objects. In order to remove objects one of the solution is to use git filter-branch. I suggest you to read this topic about removing objects and the git filter-branch documentation. Thanks for the heads up @mvp.

like image 3
Daniel Gomes Avatar answered Nov 09 '22 19:11

Daniel Gomes