简体   繁体   中英

How to compact local git repo

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.

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

在此处输入图片说明


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

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM