Skip to content

Instantly share code, notes, and snippets.

@RianFuro
Created April 30, 2023 16:30
Show Gist options
  • Save RianFuro/743efc6e929080b738461fe4df8d7db6 to your computer and use it in GitHub Desktop.
Save RianFuro/743efc6e929080b738461fe4df8d7db6 to your computer and use it in GitHub Desktop.
Strip down a repository to just it's bare minimum remote tracking information, to reduce the size on disk as much as possible.
#!/bin/sh
# Create inactive branch with empty working directory
git checkout --orphan inactive
git reset --hard
git commit --allow-empty -m 'Configured to save space. Use `git pull && git checkout master` to get back to work!'
# Remove all other branches
git branch | grep -v "^*" | xargs git branch -D
# Remove tracking information of other branches
rm -rf .git/refs/remotes/**/*
sed -i '/refs\/heads\/inactive$/!d' .git/packed-refs
sed -i '/refs\/heads\/inactive$/!d' .git/info/refs
# Clean reflog
git reflog expire --expire-unreachable=now --all
git gc --prune=now
# remove known temporary data
rm -rf node_modules/
rm -rf dist/
rm -rf vendor/
@RianFuro
Copy link
Author

This snippet assumes that all your work is committed and pushed to your upstream, as it removes the contents of your working directory and almost all tracked objects from gits internal database. The last known good state can simply be restored by fetching upstream again and checking out any branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment