Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save awfulwoman/f78f1dac139cbdbfac8a2e1a54cb0d5f to your computer and use it in GitHub Desktop.
Save awfulwoman/f78f1dac139cbdbfac8a2e1a54cb0d5f to your computer and use it in GitHub Desktop.
How to Flatten the History of a Git Repository Safely

BACK UP YOUR REPO BEFORE DOING ANYTHING LIKE THIS!!

git checkout --orphan future-main
git add -A  # Add all files and commit them
git commit -m "Initial commit"
git branch -D main  # Deletes the main branch
git branch -m main  # Rename the current branch to main
git push -f origin main  # Force push main branch to github
git gc --aggressive --prune=all     # remove the old files

As one single copy and paste command:

git checkout --orphan future-main && git add -A && git commit -m "Initial commit" && git branch -D main && git branch -m main && git push -f origin main && git gc --aggressive --prune=all 

Works well with git submodules too. From: https://stackoverflow.com/a/13102849/1253966

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