4

Can I achieve the result of git clone --depth=1 on a local already cloned repo which has all history while the aim is to keep all the ignored files in place?

I have several git projects consuming lots of space and I would like to remove all git history to release some space. Thanks.

UPDATE

To be more precise:

  • I don't want to do any manual work with moving files here and there, deleting repo, recreate, etc
  • I have some config files in assume-unchanged state which must be kept
  • I have many gitignored files which must be left in place

These reasons can make it clear why I don't want to re-clone and set up all credentials and stuff on 30 repos.

Ideally I'm looking for something I could even run in batch on all project. If there's no such thing, then I'm fine with it of course.

zsitro
  • 1,812
  • 3
  • 24
  • 34

2 Answers2

10

I have several git projects consuming lots of space and I would like to remove all git history to release some space. Thanks

Why not simply delete the projects and re-clone it again with the git clone --depth --branch ... to clone a specific branch and the latest commit as you did.


How to pack your repository:

You have to run gc to do it.

# Expire all the unreachable content and pack the repo in the most 
# compact way.
git gc --aggressive --prune=now

# also clear the reflog as well
git reflog expire --expire=now --all

Clean out large files

You should use this tool to clean your repository history:
https://rtyley.github.io/bfg-repo-cleaner/

It the prefect tool for this kind of task

BFG Repo-Cleaner

an alternative to git-filter-branch.

The BFG is a simpler, faster alternative to git-filter-branch for cleansing bad data out of your Git repository history:

  • Removing Crazy Big Files
  • Removing Passwords, Credentials & other Private data

Examples (from the official site)

In all these examples bfg is an alias for java -jar bfg.jar.

# Delete all files named 'id_rsa' or 'id_dsa' :
bfg --delete-files id_{dsa,rsa}  my-repo.git

enter image description here


After you have cleaned your repository use this tool to store your large files.

enter image description here

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • Thanks, but `git gc --aggressive --prune=now;git reflog expire --expire=now --all` could remove 20MB from `.git` while a `--depth-1` reclone is 45MB smaller – zsitro Feb 15 '16 at 14:05
  • Of course. since the gc only repack and does not remove any data while clone download only download the latest data of course. – CodeWizard Feb 15 '16 at 14:06
  • Just wanted to show you few options which you can choose and learn something new on the way. – CodeWizard Feb 15 '16 at 14:07
  • Thanks again, it helps indeed, but I leave it opened because I'm interested if others have better solutions which match more the --depth=1 repo size. – zsitro Feb 15 '16 at 17:19
2

What I would do is make a new folder, and clone the repo from the existing folder into it:

git clone --depth=1 /path/to/existingFolder /path/to/newFolder

Then copy the .git directory from /path/to/newFolder to /path/to/existingFolder, then delete /path/to/newFolder.

EDIT: according to this answer, you can edit the shallow file directly:

git show-ref -s HEAD > .git/shallow
git reflog expire --expire=0
git prune
git prune-packed
Community
  • 1
  • 1
David Deutsch
  • 17,443
  • 4
  • 47
  • 54