Skip to content

Instantly share code, notes, and snippets.

@okineadev
Last active July 16, 2025 12:40
Show Gist options
  • Save okineadev/10229a571bd2d4fcdee666085e20b271 to your computer and use it in GitHub Desktop.
Save okineadev/10229a571bd2d4fcdee666085e20b271 to your computer and use it in GitHub Desktop.
πŸ•΅οΈβ€β™‚οΈ How to hide Git commit history but keep contributions

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

πŸ“œ What does this do?

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.

πŸ“‚ Resulting commit structure (example):

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
@Kouka05
Copy link

Kouka05 commented Jul 16, 2025

This script rewrites your entire Git history to permanently hide all previous file changes and commit messages.
It preserves your commit statistics (author/dates/graph structure) by replacing every original commit with an empty [REDACTED] commit.

Finally, it adds a new archive commit containing your current files. The result is a clean history with preserved metadata but no sensitive content.

⚠️ Warning: This is destructive - force push required and collaborators must resync.

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