Skip to content

Instantly share code, notes, and snippets.

@Cdddo
Forked from heiswayi/repo-reset.md
Last active May 28, 2022 18:23
Show Gist options
  • Save Cdddo/53f02207c46966a5b0cbe6ba112208de to your computer and use it in GitHub Desktop.
Save Cdddo/53f02207c46966a5b0cbe6ba112208de to your computer and use it in GitHub Desktop.
GitHub - Delete commits history with git commands

Repo Reset

If we want to delete all of our commits history, but keep the code in its current state, try this:

# Check out to a temporary branch:
git checkout --orphan TEMP

# Commit the files:
git commit -m "Initial commit"

# Delete the main branch:
git branch -D main

# Rename the current temporary branch to main:
git branch -m main

# Force push to our repository:
git push -f origin main

# Remove the old files
git gc --aggressive --prune=all

This will not keep our old commits history around.

Note: Git will continue to store history for any commits that are referenced with branches or tags. To check, run git tag -l and git branch -v, then delete any you find. Also double check your remote with git ls-remote, you may need to delete remote tags/branches as well or when you fetch you will get all the linked files again.

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