Replies: 3 comments 1 reply
-
Very interesting! Recently I've been experimenting some advanced git commands like filter-repo 😅 they are really powerful but also scary, so I'm going slow. This is a very useful article! Thank you very much 😁 |
Beta Was this translation helpful? Give feedback.
-
Since we're talking about rebasing, here's a couple of options for
Where Then, |
Beta Was this translation helpful? Give feedback.
-
The explanation of using git filter-repo and the various filtering options is very well done. It's a powerful tool, but as you rightly point out, requires careful handling. The emphasis on cloning a mirror first is crucial. |
Beta Was this translation helpful? Give feedback.
-
Note
TL;DR Sharing field notes on Git maintenance, a preparation step for migration to Git
Git is a text-based file system for software configuration management, optimized for tracking changes and compressing text files efficiently.
Regular Git maintenance helps to:
Tip
Sometimes I am amused how people use Git beyond text files, they store binary files like snapshots, media files, backups, libraries and pakages, etc. With that, we need to do Git maintenance to remediate the performance issue.
Below are practical methods for Git maintenance, ranging from simple cleanup to advanced repository surgery.
Simple Cleanup: Delete Stale Branches or Tags
Remove unused branches or tags from the remote repository:
Advanced Cleanup: Repository Surgery
Important
Always clone a fresh copy of your repository before performing advanced operations. These actions rewrite history and are irreversible after pushing.
To clone a mirror repository for safe experimentation:
git clone --mirror YOUR-REPO-GIT-URL cd YOUR-REPO git remote set-url origin YOUR-REPO-GIT-URL
Lever 1: Migrate Large Files to Git LFS
Use Git Large File Storage (LFS) to manage large files efficiently.
Step 1: Install Git LFS:
Step 2 (Optional): Configure custom LFS storage location:
Step 3: Track specific file types with LFS:
git lfs track '*.FILE-EXTENSION'
Step 4: Migrate existing files to LFS:
git lfs migrate import --include='*.FILE-EXTENSION'
Step 5: Verify LFS-tracked files:
Lever 2: Remove Unwanted Files by Rewriting History
Use
git-filter-repo
to permanently remove unwanted files from repository history.Step 1: Install
git-filter-repo
.Step 2: Analyze repository history:
Step 3: Review large or unwanted files in
.git/filter-repo/analysis/path-all-sizes.txt
.Step 4: Remove unwanted files using filters:
Step 5 (Optional): Backup
commit-map
for reference:cp filter-repo/commit-map ./_filter_repo_commit_map_$(date +%s)
Lever 3: Drop Specific Commits
Remove unwanted commits from history using interactive rebase.
Step 1: Identify problematic commits:
With
git-sizer
With Git commands (sort by size, list top 25)
Examine a specific commit
Step 2: Start interactive rebase from a commit before the unwanted commit:
Step 3: Edit the Rebase Todo List. This will open an editor with a list of commits. The list might look something like this:
This will begin an interactive rebase from a point before the commit you want to remove.
Step 3: In the editor, mark the unwanted commit with
drop
:For example, if the commit
e4f5g6h
is the second commit to drop, you would updatedrop
next to it.Step 4: Save and exit the editor to apply changes.
Git will then replay the commits, excluding the one you marked to drop.
Lever 4: Prune Reference Logs
Clean up old or unreachable references to reduce repository size.
Option 1: Remove unreachable references immediately:
Option 2: Remove all references older than now:
Lever 5: Garbage Collection
Clean up dangling objects and optimize repository storage.
Step 1: Review unreachable and dangling objects:
Step 2: Remove dangling objects:
Step 3: Verify integrity and connectivity of all objects:
Final Step: Push Changes to Remote
After performing advanced cleanup, push to rewrite remote history:
Option 1: Rewrite all references (branches, tags, remotes):
Option 2: Rewrite all branches only:
Option 3: Rewrite specific branches:
git push origin --force 'refs/heads/*'
Option 4: Rewrite all tags only:
Option 5: Rewrite specific tags:
git push origin --force 'refs/tags/*'
Warning
Force-pushing rewrites remote history. Ensure all collaborators are informed and have synchronized their local repositories accordingly.
Beta Was this translation helpful? Give feedback.
All reactions