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

This comment has been minimized.

Copy link

splaisan commented May 15, 2017

thanks, very usefull

@sandeep0402

This comment has been minimized.

Copy link

sandeep0402 commented Jun 13, 2017

Really helpful, saved my day

@antoniocosentino

This comment has been minimized.

Copy link

antoniocosentino commented Jun 29, 2017

thanks!

@alromh87

This comment has been minimized.

Copy link

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.

Copy link

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.

Copy link

hennott commented Aug 18, 2017

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

@vgrange

This comment has been minimized.

Copy link

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.

Copy link

devrepo commented Sep 17, 2017

Thanks it was very useful

@heshanlk

This comment has been minimized.

Copy link

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.

Copy link

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.

Copy link

artykbayevk commented Oct 18, 2017

Thanks, man!

@Issen007

This comment has been minimized.

Copy link

Issen007 commented Nov 3, 2017

Great help, thanks.

@cvera08

This comment has been minimized.

Copy link

cvera08 commented Nov 30, 2017

Thank you!

@kaindad

This comment has been minimized.

Copy link

kaindad commented Dec 27, 2017

Great help! Thank you

@ilangovane

This comment has been minimized.

Copy link

ilangovane commented Jan 6, 2018

It works perfectly ! Thank you !

@gitanupam

This comment has been minimized.

Copy link

gitanupam commented Jan 25, 2018

Thanks!

@mr-speak

This comment has been minimized.

Copy link

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.

Copy link

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.

Copy link

galamach commented Apr 24, 2018

that was a great help, many thanks

@gustavomf-cit

This comment has been minimized.

Copy link

gustavomf-cit commented Jun 22, 2018

awesome! thanks for share it

@rahianeja

This comment has been minimized.

Copy link

rahianeja commented Jun 26, 2018

Wonderful ! it worked

@dj458

This comment has been minimized.

Copy link

dj458 commented Aug 6, 2018

Thank you! This one works well!

@markstos

This comment has been minimized.

Copy link

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.

Copy link

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.

Copy link

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.

Copy link

evan45245 commented Sep 13, 2018

Worked great, useful!

@aioue

This comment has been minimized.

Copy link

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.

Copy link

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.

Copy link

Miopas commented Nov 20, 2018

This worked for me. Thanks.

@rishi-shah

This comment has been minimized.

Copy link

rishi-shah commented Feb 3, 2019

Awesome. This worked for me. Thank you.

@harshblog150

This comment has been minimized.

Copy link

harshblog150 commented Feb 4, 2019

nice!

@FarhanSajid1

This comment has been minimized.

Copy link

FarhanSajid1 commented Apr 1, 2019

Thanks a ton, really appreciate this!

@guardrex

This comment has been minimized.

Copy link

guardrex commented Apr 2, 2019

Does clearing the history in this way clear the contributions as well (i.e., reset everyone to 0/zero contributions on the Contributors page)?

@Noitidart

This comment has been minimized.

Copy link

Noitidart commented Apr 26, 2019

Awesome thank you! I did a new git init added the origin remote and then git push -u --force origin master and this compltely wiped out my old github history and replaced it with this!

@wsoubar

This comment has been minimized.

Copy link

wsoubar commented May 18, 2019

Thank you!!!
you did the trick

@DoofCoder

This comment has been minimized.

Copy link

DoofCoder commented May 28, 2019

Thank you. I was badly needing it!

@LinmeiJ

This comment has been minimized.

Copy link

LinmeiJ commented Jun 1, 2019

Saved my day!!! thank you!

@Jburgin9

This comment has been minimized.

Copy link

Jburgin9 commented Jun 16, 2019

This helped so much!!!!!!!!

@Matze693

This comment has been minimized.

Copy link

Matze693 commented Jun 25, 2019

so easy, thank you 😄

@Mylab6

This comment has been minimized.

Copy link

Mylab6 commented Jun 30, 2019

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

THANK YOU , I had an issue with lfs not removing the big files correctly, this fixed it right up

@wgDev222

This comment has been minimized.

Copy link

wgDev222 commented Jul 10, 2019

thank you so much

@thubamamba

This comment has been minimized.

Copy link

thubamamba commented Jul 13, 2019

Very useful. Thank you.

@MPGAJEWSKI

This comment has been minimized.

Copy link

MPGAJEWSKI commented Jul 21, 2019

It helped me. Thanks!

@marinoguerieri

This comment has been minimized.

Copy link

marinoguerieri commented Jul 22, 2019

Thanks. I used Gatsby starter pack with bunch of commits made before I cloned it and this worked perfectly.

@paulolorenzobasilio

This comment has been minimized.

Copy link

paulolorenzobasilio commented Aug 1, 2019

Thank you!

@tonyselleurop

This comment has been minimized.

Copy link

tonyselleurop commented Aug 1, 2019

Thank you :D

@benktesh

This comment has been minimized.

Copy link

benktesh commented Aug 2, 2019

Great.

@icenamor

This comment has been minimized.

Copy link

icenamor commented Oct 4, 2019

thanks!

@YenHub

This comment has been minimized.

Copy link

YenHub commented Oct 25, 2019

Many kudos to you @stephenhardy

Thanks :)

@almeidacavalcante

This comment has been minimized.

Copy link

almeidacavalcante commented Oct 25, 2019

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

You are a genius!

@fypliawfl

This comment has been minimized.

Copy link

fypliawfl commented Nov 3, 2019

Thank you, that helped

@Richzendy

This comment has been minimized.

Copy link

Richzendy commented Nov 12, 2019

Thanks a lot! works perfectly

@gpalazzo

This comment has been minimized.

Copy link

gpalazzo commented Dec 8, 2019

works like a charm, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.