Skip to content

Instantly share code, notes, and snippets.

@stephenhardy
Created April 26, 2013 22:14
Show Gist options
  • Save stephenhardy/5470814 to your computer and use it in GitHub Desktop.
Save stephenhardy/5470814 to your computer and use it in GitHub Desktop.
Steps to clear out the history of a git/github repository
-- Remove the history from
rm -rf .git
-- recreate the repos from the current content only
git init
git add .
git commit -m "Initial commit"
-- push to the github remote repos ensuring you overwrite history
git remote add origin [email protected]:<YOUR ACCOUNT>/<YOUR REPOS>.git
git push -u --force origin master
@rileyrg
Copy link

rileyrg commented Apr 7, 2021

Above didn't work but the following worked with more attributes during the push.

git init
git add .
git commit -m 'Initial commit'
git remote add origin [repo_address]
git push --mirror --force

Thanks @heshanlk , the --mirror option was what I needed!

See here, nice script which takes care of it all maintaining the original config which is what you want: resetting github repo

@cole-wilson
Copy link

Thank you!

@ashubalike
Copy link

Great. Neat and clean. It's been very helpful.

@DavidLBrandt
Copy link

Thank you!

@aaxbas
Copy link

aaxbas commented May 18, 2021

Thanks!

@AtomicNess123
Copy link

Is it possible to delete the history from within Github? Thanks.

@quanghuyle3
Copy link

THANK YOU SO MUCH. YOU HAVE SAVED MY LIFE!

@indieshack
Copy link

Is it possible to delete the history from within Github? Thanks.

Good question - GitHub is so conservative about supporting this kind of admin it sucks. There absolutely should be a way to do this within the browser - they won't to save developer resources

@AtomicNess123
Copy link

Is it possible to delete the history from within Github? Thanks.

Good question - GitHub is so conservative about supporting this kind of admin it sucks. There absolutely should be a way to do this within the browser - they won't to save developer resources

Interesting. Do I understand however that running this git-clearHistory we will clear it?

@jennerwein
Copy link

Thank you! Great coding, simple and yet extremely effective!!

@Beyarz
Copy link

Beyarz commented Sep 20, 2021

Gold

@sharesourcecode
Copy link

This might be problematic with repositories with git submodules. I believe the recipe in this SO answer is a safer way: https://stackoverflow.com/a/13102849

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

Fantastic!
For automation it is better not to use the editor:
git commit -m 'Clear history'

@t-pollington
Copy link

t-pollington commented Oct 19, 2021

git commit

Will probably need to do git commit -m "some message" instead, to avoid an error.

@ayrokid
Copy link

ayrokid commented Nov 13, 2021

thanks you so much

@nolawnchairs
Copy link

Works fine for me. I had a customer who never finished paying me for work, so nuked the code in the remote repo and added my ransom to the README.

@mchapman87501
Copy link

...
git push --mirror --force

Thank you, @heshanlk.

@lzkill
Copy link

lzkill commented Dec 20, 2021

Adding a variable to capture the default branch name (it might be master or main):

#!/bin/bash

default_branch=`basename $(git symbolic-ref --short refs/remotes/origin/HEAD)`

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

@jrson83
Copy link

jrson83 commented Dec 27, 2021

Thanks.

@graciofilipe
Copy link

❤️

@FossPrime
Copy link

Save yourself a world of pain and use main as default branch... blame Github monopoly for causing this chaos.
In git 2.23 / replit's default / NixOS 22_05 default, use the following, as it has no concept of default branch:

git branch main
git checkout main
git branch -d master

@Code-Case
Copy link

Code-Case commented Nov 22, 2022

Using just a branch isnt a good concept for branching.

Its always depends about how your workflow looks like and then learn how to branch.

Best regards

@samih-sghier
Copy link

worked like charm

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