Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete history in local repository instead of cloning it again with --depth 1

Tags:

git

Title basically.

I just mixed some things up with my .gitignore and bloated my .git dir to > 100mb (it's a repo with like 100commits total..).

So I'm a lazy guy and was wondering if it would be possible to delete my history. I don't want to rebase something or stuff, I just want to shrink my .git dir size.

When there is no way to do this I just would git clone --depth 1 my repo to get the same effect.

like image 884
boop Avatar asked Nov 25 '15 00:11

boop


1 Answers

Those are two conflicting goals:

  • delete the history implies a brand new repo (rm -Rf .git, git init .): that means you have to force push your new local repo with a git push --force, destroying the history on the remote repo as well,

  • keeping the remote repo intact means locally working with git clone --depth 1, which is safe to do since Git 2.0, as I have documented here (ie. you can push back to a remote repo from a local shallow repo)

The second approach seems the safest for:

  • preserving the remote repo
  • optimizing disk space locally

Note: for a local repo with its full history, git gc and git repack alone are not enough to really shrink the size of a .git folder.
See "git gc --aggressive vs git repack".

git gc
git repack -Ad      # kills in-pack garbage
git prune           # kills loose garbage

(That would not do much on a freshly cloned repo, that would only be effective on a local repo you have been working for some time) Plus, if you did in the past operations like git filter-branch, your .git folder would still keep a folder like .git/refs/original/ that you need to delete if you want to reduce the size of .git/.


AFAIU the original question and how I want to do it is precisely without removing .git folder, just removing local copy of git log data to make its' state identical to the state after git clone --depth 1

See "Converting git repository to shallow?"

You can try:

cd /my/repo
git show-ref -s HEAD > .git/shallow
git reflog expire --expire=0
git prune
git prune-packed
like image 180
VonC Avatar answered Oct 26 '22 00:10

VonC