Skip to content

Instantly share code, notes, and snippets.

@okineadev
Last active April 6, 2025 07:39
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment