This Bash script is designed to clean up and optimize a Git repository by reducing its size and removing unnecessary files and objects. Below is a summary of its functionalities:
Usage: ./git-clean.sh [-f] [-b branch_name]
-f: Force aggressive cleanup (use with caution)
-b branch_name: Specify the branch to clean (default: current branch)
-
Fetch Latest Changes:
The script fetches all branches and prunes any branches that no longer exist on the remote. -
Switch Branch:
It checks out the specified branch (or remains on the current branch if none is specified). -
Garbage Collection:
Performs Git's garbage collection in aggressive mode to remove unreachable objects and optimize the repository. -
Reflog Cleanup:
Expires all reflog entries immediately, removing old references. -
File Cleanup:
Removes untracked files and directories, excluding those ignored by.gitignore
.
-
Forceful File Removal:
Removes all untracked files, directories, and ignored files (use with caution). -
Repository Repackaging:
Repackages the repository with specified depth and window size for better compression. -
Object Pruning:
Removes all unreachable objects that are not part of any branch or tag. -
History Rewriting:
Aggressively rewrites the repository history by removing cached.log
files (use with extreme caution as this is irreversible).
- Database Optimization:
Optimizes the Git repository database for better performance.
- Safe Force Push:
Forcefully pushes changes to the remote repository using--force-with-lease
to prevent overwriting others' work.
- Size Calculation:
The script calculates and displays the initial and final sizes of the repository to show the impact of the cleanup.
- Post-Cleanup Reminder:
The script reminds the user to possibly force push to the remote and ask their Git host to run garbage collection for further size reduction.
-
Aggressive Cleanup:
The-f
option enables aggressive cleanup, which includes irreversible actions like history rewriting and forceful file removal. Use this option with extreme caution. -
Remote Size Reduction:
To fully realize size reductions on the remote repository, you may need to force push the changes and request garbage collection from your Git host.