Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 git@github.com:<YOUR ACCOUNT>/<YOUR REPOS>.git
git push -u --force origin master
@Mark-Edwards-II
Copy link

Mark-Edwards-II commented Nov 18, 2020

I can now sleep at night, Thank you!

@step2csonline
Copy link

step2csonline commented Dec 14, 2020

thanks

@Code-Case
Copy link

Code-Case commented Dec 30, 2020

helped me remove my weird commits

you could also use rebase or squash to clean up weird commitlogs

@rileyrg
Copy link

rileyrg commented Jan 4, 2021

tfile=$(mktemp /tmp/config.XXXXXXXXX)
GITCONF=".git/config"
commitmsg=${1:-git repository initialised}
if [ -f "$GITCONF" ]; then
mv "$GITCONF" tfile
rm -rf .git
git init .
mv tfile "$GITCONF"
git add .
git commit -a -m "${commitmsg}"
git push -f
else
echo "Warning: No git config file found. Aborting.";exit;
fi

@Hassan829
Copy link

Hassan829 commented Jan 16, 2021

Thank you

@fanon
Copy link

fanon commented Jan 28, 2021

Thanks!

@leoyala
Copy link

leoyala commented Mar 19, 2021

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

This worked perfectly for me, thanks @adeluccar

@johnwry
Copy link

johnwry commented Mar 21, 2021

this is so frustrating...git was made so easy it's literally impossible to do anything.

@pushkatel
Copy link

pushkatel commented Mar 31, 2021

Any easy way to clear git history before a certain date? I just want to keep the relevant commits since my team started working on the project, rather than commits to a 'boilermaker' code we used

@conschneider
Copy link

conschneider commented Apr 6, 2021

Worked for me. Thanks!

@ganeshh123
Copy link

ganeshh123 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!

@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

cole-wilson commented Apr 17, 2021

Thank you!

@ashubalike
Copy link

ashubalike commented Apr 27, 2021

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

@DavidLBrandt
Copy link

DavidLBrandt commented May 12, 2021

Thank you!

@aaxbas
Copy link

aaxbas commented May 18, 2021

Thanks!

@AtomicNess123
Copy link

AtomicNess123 commented May 28, 2021

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

@quanghuyle3
Copy link

quanghuyle3 commented Jun 4, 2021

THANK YOU SO MUCH. YOU HAVE SAVED MY LIFE!

@indieshack
Copy link

indieshack commented Jun 25, 2021

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

AtomicNess123 commented Jun 25, 2021

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?

@jhongia
Copy link

jhongia commented Jul 16, 2021

Thank you!!!

@jennerwein
Copy link

jennerwein commented Jul 28, 2021

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

@Beyarz
Copy link

Beyarz commented Sep 20, 2021

Gold

@sharesourcecode
Copy link

sharesourcecode commented Sep 29, 2021

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

nolawnchairs commented Nov 13, 2021

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

mchapman87501 commented Nov 16, 2021

...
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.

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