Instantly share code, notes, and snippets.

Embed
What would you like to do?
GitHub - Delete commits history with git commands

First Method

Deleting the .git folder may cause problems in our git repository. If we want to delete all of our commits history, but keep the code in its current state, try this:

# Check out to a temporary branch:
git checkout --orphan TEMP_BRANCH

# Add all the files:
git add -A

# Commit the changes:
git commit -am "Initial commit"

# Delete the old branch:
git branch -D master

# Rename the temporary branch to master:
git branch -m master

# Finally, force update to our repository:
git push -f origin master

This will not keep our old commits history around. But if this doesn't work, try the next method below.

Second Method

# Clone the project, e.g. `myproject` is my project repository:
git clone https://github/heiswayi/myproject.git

# Since all of the commits history are in the `.git` folder, we have to remove it:
cd myproject

# And delete the `.git` folder:
git rm -rf .git

# Now, re-initialize the repository:
git init
git remote add origin https://github.com/heiswayi/myproject.git
git remote -v

# Add all the files and commit the changes:
git add --all
git commit -am "Initial commit"

# Force push update to the master branch of our project repository:
git push -f origin master

NOTE: You might need to provide the credentials for your GitHub account.

@ertugrulcetin

This comment has been minimized.

Show comment
Hide comment
@ertugrulcetin

ertugrulcetin commented Sep 13, 2017

Thank you!

@prageet

This comment has been minimized.

Show comment
Hide comment
@prageet

prageet Sep 23, 2017

Thank you.

prageet commented Sep 23, 2017

Thank you.

@baGoodQE

This comment has been minimized.

Show comment
Hide comment
@baGoodQE

baGoodQE Sep 30, 2017

First method worked, thanks for sharing!

baGoodQE commented Sep 30, 2017

First method worked, thanks for sharing!

@jayeshdshah

This comment has been minimized.

Show comment
Hide comment
@jayeshdshah

jayeshdshah Oct 12, 2017

while executing second method, it shown below error message
pathspec '.git' did not match any files

jayeshdshah commented Oct 12, 2017

while executing second method, it shown below error message
pathspec '.git' did not match any files

@xiongmai

This comment has been minimized.

Show comment
Hide comment
@xiongmai

xiongmai Oct 18, 2017

yay thanks! I used the first method.

xiongmai commented Oct 18, 2017

yay thanks! I used the first method.

@Rob-van-B

This comment has been minimized.

Show comment
Hide comment
@Rob-van-B

Rob-van-B commented Oct 24, 2017

great!

@DoctorCookie

This comment has been minimized.

Show comment
Hide comment
@DoctorCookie

DoctorCookie commented Nov 9, 2017

thanks

@he-dev

This comment has been minimized.

Show comment
Hide comment
@he-dev

he-dev Nov 19, 2017

Thank you! The first method worked for me ;-)

he-dev commented Nov 19, 2017

Thank you! The first method worked for me ;-)

@jasonmeurer

This comment has been minimized.

Show comment
Hide comment
@jasonmeurer

jasonmeurer Dec 4, 2017

For those using the second method, remove 'git' from the beginning of the rm command.

And delete the .git folder:

rm -rf .git

jasonmeurer commented Dec 4, 2017

For those using the second method, remove 'git' from the beginning of the rm command.

And delete the .git folder:

rm -rf .git

@kirkvicent

This comment has been minimized.

Show comment
Hide comment
@kirkvicent

kirkvicent Dec 15, 2017

多谢多谢
from china

kirkvicent commented Dec 15, 2017

多谢多谢
from china

@Anihilakos

This comment has been minimized.

Show comment
Hide comment
@Anihilakos

Anihilakos commented Dec 15, 2017

thank you!

@maxdonetsk

This comment has been minimized.

Show comment
Hide comment
@maxdonetsk

maxdonetsk Dec 27, 2017

Works like a charm. Thanks!

maxdonetsk commented Dec 27, 2017

Works like a charm. Thanks!

@Zibri

This comment has been minimized.

Show comment
Hide comment
@Zibri

Zibri Dec 28, 2017

error: git rm -rf .git >>> rm -rf .git

Zibri commented Dec 28, 2017

error: git rm -rf .git >>> rm -rf .git

@moshfeu

This comment has been minimized.

Show comment
Hide comment
@moshfeu

moshfeu commented Jan 10, 2018

Thanks!

@sebamawa

This comment has been minimized.

Show comment
Hide comment
@sebamawa

sebamawa Feb 6, 2018

First method ok! . Thanks!

sebamawa commented Feb 6, 2018

First method ok! . Thanks!

@mohammadmahdikarimi

This comment has been minimized.

Show comment
Hide comment
@mohammadmahdikarimi

mohammadmahdikarimi Feb 14, 2018

I am completely new and it worked for me. Thanks

mohammadmahdikarimi commented Feb 14, 2018

I am completely new and it worked for me. Thanks

@Gabboxl

This comment has been minimized.

Show comment
Hide comment
@Gabboxl

Gabboxl Apr 24, 2018

Omg, Thanks bro!

Gabboxl commented Apr 24, 2018

Omg, Thanks bro!

@FadeMind

This comment has been minimized.

Show comment
Hide comment
@FadeMind

FadeMind Apr 28, 2018

rm -rf .git
git init
git remote add origin git@github.com:JohnDoe/foobar.git
git remote -v
git add --all
git commit -am "Initial commit"
git push -f origin master

FadeMind commented Apr 28, 2018

rm -rf .git
git init
git remote add origin git@github.com:JohnDoe/foobar.git
git remote -v
git add --all
git commit -am "Initial commit"
git push -f origin master
@maxlefou

This comment has been minimized.

Show comment
Hide comment
@maxlefou

maxlefou Jun 15, 2018

First method didn't worked on my private git server. remote: error: denying non-fast-forward refs/heads/master (you should pull first)

maxlefou commented Jun 15, 2018

First method didn't worked on my private git server. remote: error: denying non-fast-forward refs/heads/master (you should pull first)

@lucasbuetto

This comment has been minimized.

Show comment
Hide comment
@lucasbuetto

lucasbuetto commented Jun 20, 2018

Thanks Bro!

@Alexiades

This comment has been minimized.

Show comment
Hide comment
@Alexiades

Alexiades Jul 2, 2018

Thank you!! first method works perfect!

Alexiades commented Jul 2, 2018

Thank you!! first method works perfect!

@greg-amato

This comment has been minimized.

Show comment
Hide comment
@greg-amato

greg-amato commented Jul 26, 2018

Thanks!

@IndraMahkota

This comment has been minimized.

Show comment
Hide comment
@IndraMahkota

IndraMahkota commented Jul 31, 2018

thanks

@vserrao

This comment has been minimized.

Show comment
Hide comment
@vserrao

vserrao commented Aug 9, 2018

Thanks!

@algenisromero

This comment has been minimized.

Show comment
Hide comment
@algenisromero

algenisromero commented Aug 12, 2018

Thanks!

@arharo

This comment has been minimized.

Show comment
Hide comment
@arharo

arharo Aug 21, 2018

Thanks mate!

arharo commented Aug 21, 2018

Thanks mate!

@danielparker00

This comment has been minimized.

Show comment
Hide comment
@danielparker00

danielparker00 commented Sep 3, 2018

Thanks.

@bernardowb

This comment has been minimized.

Show comment
Hide comment
@bernardowb

bernardowb Sep 4, 2018

Second method ok (the only I've tested)! Thanks!

bernardowb commented Sep 4, 2018

Second method ok (the only I've tested)! Thanks!

@MarkusL27

This comment has been minimized.

Show comment
Hide comment
@MarkusL27

MarkusL27 Sep 23, 2018

Thanks! Works perfect!

MarkusL27 commented Sep 23, 2018

Thanks! Works perfect!

@Daltonic

This comment has been minimized.

Show comment
Hide comment
@Daltonic

Daltonic Sep 23, 2018

Thanks Man

Daltonic commented Sep 23, 2018

Thanks Man

@ShinekaBarnett

This comment has been minimized.

Show comment
Hide comment
@ShinekaBarnett

ShinekaBarnett commented Oct 17, 2018

Thanks!

@DianaLauraSaavedra

This comment has been minimized.

Show comment
Hide comment
@DianaLauraSaavedra

DianaLauraSaavedra Oct 18, 2018

donde se hace todo ese metodo

DianaLauraSaavedra commented Oct 18, 2018

donde se hace todo ese metodo

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