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 splaisan commented May 15, 2017

thanks, very usefull

@sandeep0402

This comment has been minimized.

Copy link

@sandeep0402 sandeep0402 commented Jun 13, 2017

Really helpful, saved my day

@antoniocosentino

This comment has been minimized.

Copy link

@antoniocosentino antoniocosentino commented Jun 29, 2017

thanks!

@alromh87

This comment has been minimized.

Copy link

@alromh87 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 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 hennott commented Aug 18, 2017

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

@vgrange

This comment has been minimized.

Copy link

@vgrange 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 devrepo commented Sep 17, 2017

Thanks it was very useful

@heshanlk

This comment has been minimized.

Copy link

@heshanlk 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 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 artykbayevk commented Oct 18, 2017

Thanks, man!

@Issen007

This comment has been minimized.

Copy link

@Issen007 Issen007 commented Nov 3, 2017

Great help, thanks.

@cvera08

This comment has been minimized.

Copy link

@cvera08 cvera08 commented Nov 30, 2017

Thank you!

@kaindad

This comment has been minimized.

Copy link

@kaindad kaindad commented Dec 27, 2017

Great help! Thank you

@ilangovane

This comment has been minimized.

Copy link

@ilangovane ilangovane commented Jan 6, 2018

It works perfectly ! Thank you !

@gitanupam

This comment has been minimized.

Copy link

@gitanupam gitanupam commented Jan 25, 2018

Thanks!

@mr-speak

This comment has been minimized.

Copy link

@mr-speak 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 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 galamach commented Apr 24, 2018

that was a great help, many thanks

@gustavomf-cit

This comment has been minimized.

Copy link

@gustavomf-cit gustavomf-cit commented Jun 22, 2018

awesome! thanks for share it

@rahianeja

This comment has been minimized.

Copy link

@rahianeja rahianeja commented Jun 26, 2018

Wonderful ! it worked

@dj458

This comment has been minimized.

Copy link

@dj458 dj458 commented Aug 6, 2018

Thank you! This one works well!

@markstos

This comment has been minimized.

Copy link

@markstos 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 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 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?
@eyeseaevan

This comment has been minimized.

Copy link

@eyeseaevan eyeseaevan commented Sep 13, 2018

Worked great, useful!

@aioue

This comment has been minimized.

Copy link

@aioue 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 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 Miopas commented Nov 20, 2018

This worked for me. Thanks.

@rishi-shah

This comment has been minimized.

Copy link

@rishi-shah rishi-shah commented Feb 3, 2019

Awesome. This worked for me. Thank you.

@harshblog150

This comment has been minimized.

Copy link

@harshblog150 harshblog150 commented Feb 4, 2019

nice!

@FarhanSajid1

This comment has been minimized.

Copy link

@FarhanSajid1 FarhanSajid1 commented Apr 1, 2019

Thanks a ton, really appreciate this!

@guardrex

This comment has been minimized.

Copy link

@guardrex 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 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 wsoubar commented May 18, 2019

Thank you!!!
you did the trick

@DoofCoder

This comment has been minimized.

Copy link

@DoofCoder DoofCoder commented May 28, 2019

Thank you. I was badly needing it!

@LinmeiJ

This comment has been minimized.

Copy link

@LinmeiJ LinmeiJ commented Jun 1, 2019

Saved my day!!! thank you!

@Jburgin9

This comment has been minimized.

Copy link

@Jburgin9 Jburgin9 commented Jun 16, 2019

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

@Matze693

This comment has been minimized.

Copy link

@Matze693 Matze693 commented Jun 25, 2019

so easy, thank you 😄

@Mylab6

This comment has been minimized.

Copy link

@Mylab6 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 wgDev222 commented Jul 10, 2019

thank you so much

@thubamamba

This comment has been minimized.

Copy link

@thubamamba thubamamba commented Jul 13, 2019

Very useful. Thank you.

@MPGAJEWSKI

This comment has been minimized.

Copy link

@MPGAJEWSKI MPGAJEWSKI commented Jul 21, 2019

It helped me. Thanks!

@marinoguerieri

This comment has been minimized.

Copy link

@marinoguerieri 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 paulolorenzobasilio commented Aug 1, 2019

Thank you!

@tonyselleurop

This comment has been minimized.

Copy link

@tonyselleurop tonyselleurop commented Aug 1, 2019

Thank you :D

@benktesh

This comment has been minimized.

Copy link

@benktesh benktesh commented Aug 2, 2019

Great.

@icenamor

This comment has been minimized.

Copy link

@icenamor icenamor commented Oct 4, 2019

thanks!

@YenHub

This comment has been minimized.

Copy link

@YenHub YenHub commented Oct 25, 2019

Many kudos to you @stephenhardy

Thanks :)

@almeidacavalcante

This comment has been minimized.

Copy link

@almeidacavalcante 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 fypliawfl commented Nov 3, 2019

Thank you, that helped

@Richzendy

This comment has been minimized.

Copy link

@Richzendy Richzendy commented Nov 12, 2019

Thanks a lot! works perfectly

@gpalazzo

This comment has been minimized.

Copy link

@gpalazzo gpalazzo commented Dec 8, 2019

works like a charm, thank you!

@mephistoc

This comment has been minimized.

Copy link

@mephistoc mephistoc commented Dec 25, 2019

really useful, thanks!!

@Zibri

This comment has been minimized.

Copy link

@Zibri Zibri commented Jan 20, 2020

@Pengwin83

This comment has been minimized.

Copy link

@Pengwin83 Pengwin83 commented Feb 22, 2020

Exactly, what I needed. Thanks ( Its bad YADA YADA, but when testing, things happen.)

@vishwa-tailored-brands

This comment has been minimized.

Copy link

@vishwa-tailored-brands vishwa-tailored-brands commented Feb 28, 2020

very helpful thanks!

@hongjonghwa

This comment has been minimized.

Copy link

@hongjonghwa hongjonghwa commented Mar 4, 2020

Thank you! Perfect!

@guangzan

This comment has been minimized.

Copy link

@guangzan guangzan commented Mar 25, 2020

thanks!

@rafamsilva

This comment has been minimized.

Copy link

@rafamsilva rafamsilva commented Mar 25, 2020

thanks!

@felipendc

This comment has been minimized.

Copy link

@felipendc felipendc commented Mar 27, 2020

I LOVE YOUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUu!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

@felipendc

This comment has been minimized.

Copy link

@felipendc felipendc commented Mar 27, 2020

I WORKED LIKE A DREAM!!!!!!!!!!!!!!!

@atfuture7

This comment has been minimized.

Copy link

@atfuture7 atfuture7 commented Mar 30, 2020

very helpful! thank you!

@markusbjorklund

This comment has been minimized.

Copy link

@markusbjorklund markusbjorklund commented Apr 7, 2020

this just came in handy. will fork!

@calcarlson

This comment has been minimized.

Copy link

@calcarlson calcarlson commented Apr 8, 2020

I am not very experienced with git and am now having all kinds of problems with every local repository command I try after trying to delete the history on a repo that I had published a personal API key in. Some common errors I am encountering.

"git pull
There is no tracking information for the current branch."

"git push
fatal: The current branch master has no upstream branch."

"fatal: refusing to merge unrelated histories"

Could someone guide me through some solutions? Sorry if this is an easy fix, I have tried many different fixes on numerous forums to no avail. Any help would be greatly appreciated.

@bedhabits

This comment has been minimized.

Copy link

@bedhabits bedhabits commented May 1, 2020

Nice summary ! Thanks

@jimshalo10

This comment has been minimized.

Copy link

@jimshalo10 jimshalo10 commented May 31, 2020

This is excellent. Thank you so much. Such a time saver as it allows a total break from the original master.
Keeps the original over 1000 files safe.

@dankger

This comment has been minimized.

Copy link

@dankger dankger commented Jun 25, 2020

Great script! Thanks for sharing!, I was looking for this for a while.
I did some changes to automatically detect the files previously added and get the repository URL. Hope you don't mind (:
So I can use it in any folder containing the .git directory and will adapt to it. You just need to add the script to a PATH folder, like

/usr/bin/git-clearHistory

#!/bin/bash

REPO=$(git config --get remote.origin.url)
FILES=$(git ls-files)

rm -rf .git
echo "Cleaning repository: $REPO"

#recreate the repos from the current content only
git init

for fl in $(echo $FILES);do
    git add $fl
done

git commit -m "Initial commit"

#push to the github remote repos ensuring you overwrite history
git remote add origin $REPO
git push -u --force origin master
@ChrisMattSam

This comment has been minimized.

Copy link

@ChrisMattSam ChrisMattSam commented Jul 16, 2020

Just what I needed; thank you

@mitchellkrogza

This comment has been minimized.

Copy link

@mitchellkrogza mitchellkrogza commented Aug 14, 2020

Works 100% thank you

@zosrothko

This comment has been minimized.

Copy link

@zosrothko zosrothko commented Aug 21, 2020

Perfect !!!

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

Under DOS, use: rd /q /s .git

@matymad

This comment has been minimized.

Copy link

@matymad matymad commented Aug 22, 2020

Thank you!!!!!

@PhamTruongHung

This comment has been minimized.

Copy link

@PhamTruongHung PhamTruongHung commented Aug 28, 2020

Thank you!!!

@wt-techachievers

This comment has been minimized.

Copy link

@wt-techachievers wt-techachievers commented Sep 3, 2020

Thank you!! Much helpful and saved may day.

@ajeetgautam31

This comment has been minimized.

Copy link

@ajeetgautam31 ajeetgautam31 commented Sep 9, 2020

Thank you very much. It saved me. :)

@omerhijazi404

This comment has been minimized.

Copy link

@omerhijazi404 omerhijazi404 commented Sep 10, 2020

helped me remove my weird commits

@tovbinm

This comment has been minimized.

Copy link

@tovbinm tovbinm commented Sep 18, 2020

@adeluccar thank you!!

@HoriSun

This comment has been minimized.

Copy link

@HoriSun HoriSun commented Oct 13, 2020

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