Skip to content

Instantly share code, notes, and snippets.

@cmin764
Created June 25, 2024 07:45
Show Gist options
  • Select an option

  • Save cmin764/63855443b6e846bbed1dd58cf4bed45c to your computer and use it in GitHub Desktop.

Select an option

Save cmin764/63855443b6e846bbed1dd58cf4bed45c to your computer and use it in GitHub Desktop.
Cleanup entire repo history
Clone the Repository
If you haven't already, clone the repository to your local machine:
bash
Copy code
git clone https://github.com/username/repository.git
cd repository
Check Out the Master Branch
Ensure you are on the master branch and have the latest changes:
bash
Copy code
git checkout master
git pull origin master
Create a New Branch (Optional)
This step is optional but recommended to ensure that the master branch can be reset without losing the current state if something goes wrong.
bash
Copy code
git checkout -b new-start
Delete All Local History
Reset the branch to the first commit, then soft reset to head. This will keep all the files but will remove the commit history:
bash
Copy code
git checkout --orphan temp_branch
git add -A
git commit -m "Initial commit"
Delete the Old Branches
At this point, temp_branch has the state of your master branch but with only one commit. Now you can delete the old master branch and rename temp_branch to master:
bash
Copy code
git branch -D master
git branch -m master
Force Push to Remote Repository
Since you have rewritten the history, you will need to force push these changes to the remote repository. This step will overwrite the history on the remote master branch with your new single-commit history.
bash
Copy code
git push -f origin master
Warning: This step is destructive and will permanently erase the repository's history. Anyone who has cloned the repository will need to re-clone it or reset their local history to match the remote.
Clean up Any Remaining Old References
If there were other references (tags, other branches), you may also want to clean them up:
bash
Copy code
git push origin --delete <old_branch>
git tag | xargs -n 1 git push --delete origin
git tag -d $(git tag)
This process will leave you with a repository that looks as it did at the latest master commit, but with all previous history removed. This is often done for making a repository smaller or removing unwanted data from earlier commits.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment