DEV Community

Prashant Iyenga
Prashant Iyenga

Posted on • Originally published at Medium on

Erasing Git History: How to Keep Only Your Latest Commit on GitHub

Have you ever committed sensitive data by mistake? Or maybe you want to wipe out your repository’s tangled history and start with a clean slate while preserving the current codebase? This guide walks you through the safe and deliberate process of removing all Git history and retaining only the latest commit on GitHub.

📌 Why Would You Want to Do This?

There are valid and common scenarios where this is useful:

  • 🔐 You’ve committed secrets (e.g., API keys) in the past.
  • 🧪 Your repository history is too messy or experimental.
  • 🚀 You want a fresh start for open-source release.
  • 🎯 You’re transitioning from a prototype to production.

Important: This is a destructive action. It will wipe out all previous commit history , including tags, branches, and pull requests. Only proceed if you understand the implications.

🚧 What You’ll Achieve

By the end of this process:

  • The repository will contain only one commit  — the current state of the code.
  • All previous commits will be irreversibly removed.
  • The branch history will be rewritten and force-pushed to GitHub.

🛠️ Step-by-Step: Clean Your GitHub Repo History

✅ 1. Clone Your Repository (Optional but Recommended)

Before you rewrite history, it’s good to work in a fresh clone to avoid local conflicts.

git clone https://github.com/your-username/your-repo.git
cd your-repo
Enter fullscreen mode Exit fullscreen mode

🌿 2. Create a New Orphan Branch

An orphan branch is a Git branch with no previous history. This is the clean slate.

git checkout --orphan latest-commit
Enter fullscreen mode Exit fullscreen mode

This command:

  • Creates a new branch.
  • Starts it with no parents (no history).

🧹 3. Stage and Commit All Files

Now, commit everything currently in your working directory:

git add -A
git commit -m "Initial commit with cleaned history"
Enter fullscreen mode Exit fullscreen mode

This creates a brand-new commit that captures your current code state.

🔥 4. Delete the Old Branch

Next, delete the existing branch (usually main or master) that contains the unwanted history.

git branch -D main # or 'master', depending on your repo
Enter fullscreen mode Exit fullscreen mode

📝 5. Rename the New Branch to main

Give your new orphan branch the same name as the one you deleted.

git branch -m main
Enter fullscreen mode Exit fullscreen mode

🚀 6. Force Push to GitHub

Finally, force push the cleaned branch to GitHub. This replaces the remote history.

git push -f origin main
Enter fullscreen mode Exit fullscreen mode

🛑 Warning : Collaborators will need to re-clone the repo. This push rewrites history for everyone.

🛡️ Aftermath: Clean-Up and Best Practices

Once you’ve rewritten history:

  • 🔐 Revoke and rotate any secrets that were exposed in the past.
  • 🕵️‍♀️ Scan your repo using tools like GitLeaks, TruffleHog, or detect-secrets.
  • 📄 Add a .gitignore file to prevent re-adding sensitive or build files.
  • 🧪 Use pre-commit hooks to lint and scan files before they’re staged.

📚 Further Reading

✅ Conclusion

Wiping a GitHub repository’s history is a powerful tool that should be used with caution. Whether you’re mitigating a leak or simplifying your project’s start point, this guide helps you do so responsibly and effectively.


Top comments (0)