Hi, I had this problem today - I had an old repository from when I was just starting to work with Git, and I needed to hide the change history because there was information in a certain commit that I didn't want to disclose.
But I have a lot of commits there, and I don't want to lose my beloved commit statistics, so how do I solve this? π€
To do this, we need to edit all commits in the repository and hide the changes and change commit names to [REDACTED]
Here is the script for that π
# Save the current commit hash to a variable so we can go back to it later
original_commit=$(git rev-parse HEAD)
# Rewrite Git history by replacing every commit's content with an empty tree
FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch \
--index-filter 'git rm --cached -r .' \ # Remove all files from the index (staging area)
--commit-filter 'git commit-tree -m "[REDACTED]" "$@"' \ # Replace commit messages with "[REDACTED]" and preserve tree structure
--tag-name-filter cat \ # Keep tags as-is
-- --all # Apply to all branches and tags
# Restore the files from the original commit to the working directory
git checkout $original_commit -- .
# Add all restored files to the staging area
git add -A
# Create a new commit with all the original content, marking it as an archive
git commit -m "archive"
# Force-push the rewritten history to the remote repository (e.g. GitHub)
git push --force
This script rewrites the entire Git history, erasing the contents of every previous commit, leaving them empty and labeled as [REDACTED]
. Then, it adds all current files as a fresh single commit called archive
on top of that clean history.
Before:
* c3d9ab2 (HEAD -> main) Add user authentication
* b8f2ac9 Setup backend routes
* 7e0b39e Initial commit
After:
* a1b2c3d (HEAD -> main) archive <-- Only real commit with content
* e4f5g6h [REDACTED] <-- Empty commit
* i7j8k9l [REDACTED] <-- Empty commit
* m0n1o2p [REDACTED] <-- Empty commit