DEV Community

Cover image for How to Remove All Git Commits Locally Without Losing Your Code
K-kibet for Codespear

Posted on

How to Remove All Git Commits Locally Without Losing Your Code

When working on a project, there are times you may want to completely remove your Git history — maybe your commit log is messy, you accidentally pushed sensitive details, or you simply want to start fresh. The good news is that you can wipe out every Git commit in a local repository without losing a single line of your code.

In this guide, we’ll walk through a simple and safe way to remove all Git commits locally and start over with clean version control.


## Why Remove Git History?

There are several good reasons developers reset their Git history:

  • To clean up messy commits and start fresh.
  • To remove accidentally committed files, like environment variables or API keys.
  • To prepare a project for open-source release without exposing past work.
  • To reduce repository size.
  • To disconnect the project from a previous remote repository.

Whatever your reason is, the process is straightforward.


Method 1: Remove Git Completely (Keep Only Your Code)

If you want to detach the project from Git entirely, use this method. It deletes all Git tracking and history but keeps all your project files safe.

Step 1: Delete the .git directory

On macOS/Linux:

rm -rf .git
Enter fullscreen mode Exit fullscreen mode

On Windows (PowerShell):

Remove-Item -Recurse -Force .git
Enter fullscreen mode Exit fullscreen mode

Once the .git folder is gone, the project is no longer a Git repository. None of your files are deleted.


Method 2: Reset Git and Start a Fresh Repo

If you still want the project under Git but with a completely clean commit history, follow these steps:

Step 1: Delete the Git history

rm -rf .git
Enter fullscreen mode Exit fullscreen mode

Step 2: Reinitialize a new Git repository

git init
Enter fullscreen mode Exit fullscreen mode

Step 3: Add all your existing files

git add .
Enter fullscreen mode Exit fullscreen mode

Step 4: Create the first clean commit

git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode

This gives you a brand-new repository with your full project files but zero previous commits.


Optional: Reconnect to a Remote Repository

If you want to push the fresh repo to GitHub, GitLab, or Bitbucket, add your remote:

git remote add origin your-repo-url.git
git push -u --force origin main
Enter fullscreen mode Exit fullscreen mode

⚠️ Warning: Using --force will overwrite the remote history.


Final Thoughts

Clearing your Git history is a powerful way to clean up your project, enhance privacy, or prepare your code for a new workflow. The best part? You don’t lose any of your files — only the commit history is wiped.

Whether you’re cleaning up a personal project or preparing for a professional deployment, starting fresh with a clean Git history can make your repository easier to understand and maintain.

Top comments (0)