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
@splaisan

This comment has been minimized.

splaisan commented May 15, 2017

thanks, very usefull

@sandeep0402

This comment has been minimized.

sandeep0402 commented Jun 13, 2017

Really helpful, saved my day

@antoniocosentino

This comment has been minimized.

antoniocosentino commented Jun 29, 2017

thanks!

@alromh87

This comment has been minimized.

alromh87 commented Jul 10, 2017

Im afraid you can still reference specific "deleted" commits by hash, eventough they don't appear in history

@adeluccar

This comment has been minimized.

adeluccar commented Aug 4, 2017

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
@hennott

This comment has been minimized.

hennott commented Aug 18, 2017

This saved my day .. an is working "nearly" automatic.

@vgrange

This comment has been minimized.

vgrange commented Aug 24, 2017

Warning : this does not delete pull request history (discussion, file changes). If you do this in order to delete sensitive data, you should better delete the github repo (along with all its pull request history etc..) and recreate a new one from scratch.

@devrepo

This comment has been minimized.

devrepo commented Sep 17, 2017

Thanks it was very useful

@heshanlk

This comment has been minimized.

heshanlk commented Oct 10, 2017

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

@jayeshdshah

This comment has been minimized.

jayeshdshah commented Oct 12, 2017

Perfect !!!

Just a small comment :
rm -rf .git <------ use this command from git bash.. as i was initially using dos command prompt

@artykbayevk

This comment has been minimized.

artykbayevk commented Oct 18, 2017

Thanks, man!

@Issen007

This comment has been minimized.

Issen007 commented Nov 3, 2017

Great help, thanks.

@cvera08

This comment has been minimized.

cvera08 commented Nov 30, 2017

Thank you!

@kaindad

This comment has been minimized.

kaindad commented Dec 27, 2017

Great help! Thank you

@ilangovane

This comment has been minimized.

ilangovane commented Jan 6, 2018

It works perfectly ! Thank you !

@gitanupam

This comment has been minimized.

gitanupam commented Jan 25, 2018

Thanks!

@mr-speak

This comment has been minimized.

mr-speak commented Mar 6, 2018

This did work for me, but initially I ran into problems after the two last lines (with my account and repo name, of course):

git remote add origin git@github.com:<YOUR ACCOUNT>/<YOUR REPOS>.git
git push -u --force origin master

After running the last command, which failed, I started to get error messages for every command I used. I was not allowed to push. For example:

git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

In my case, the solution was to follow this guide to use HTTPS instead of SSH:
https://help.github.com/articles/changing-a-remote-s-url/

@paulxtiseo

This comment has been minimized.

paulxtiseo commented Apr 13, 2018

@heshanik, the mirror tag is exactly what I was looking for. Wanted to make sure Github looked like my local repo. Thanks.

@galamach

This comment has been minimized.

galamach commented Apr 24, 2018

that was a great help, many thanks

@gustavomf-cit

This comment has been minimized.

gustavomf-cit commented Jun 22, 2018

awesome! thanks for share it

@rahianeja

This comment has been minimized.

rahianeja commented Jun 26, 2018

Wonderful ! it worked

@dj458

This comment has been minimized.

dj458 commented Aug 6, 2018

Thank you! This one works well!

@markstos

This comment has been minimized.

markstos commented Aug 6, 2018

I believe you need to delete and recreate the Github remote repo to completely remove the logs. See for example Recovering a commit from Githubs reflog.

Using the Github API described there, you access commits that are stored on Github don't appear in the normal commit history on Github.

With this method, you don't know have to know the SHA of a commit you are looking for.

Also, if the project in question is public, it appears the API calls to review the history are also public.

@david-j-m

This comment has been minimized.

david-j-m commented Aug 19, 2018

Thanks Heshanlk this worked for me, after adding

git remote rm origin

before the line

git remote add origin [repro address]

'cause I was receiving:
fatal: remote origin already exists

@PranoopMutha

This comment has been minimized.

PranoopMutha commented Aug 27, 2018

  1. I want to do it from the github desktop. Is there any way??
  2. Can some one give the final commands which worked to remove history? Are these commands need to be typed from gitbash or command prompt?
@evan45245

This comment has been minimized.

evan45245 commented Sep 13, 2018

Worked great, useful!

@aioue

This comment has been minimized.

aioue commented Sep 20, 2018

for a gist the add orgin looks like git remote add origin git@gist.github.com:38238742938479283iuweyrwieury83472837.git

@jeffrade

This comment has been minimized.

jeffrade commented Oct 22, 2018

If you want an empty repom then pass the --allow-empty flag on commit:

git commit -S -m "Initial commit" --allow-empty

or else you'll get the following message:

On branch master

Initial commit

nothing to commit
@Miopas

This comment has been minimized.

Miopas commented Nov 20, 2018

This worked for me. Thanks.

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