Skip to content

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.

Copy link

@ertugrulcetin ertugrulcetin commented Sep 13, 2017

Thank you!

@prageet

This comment has been minimized.

Copy link

@prageet prageet commented Sep 23, 2017

Thank you.

@BrianKSFDO

This comment has been minimized.

Copy link

@BrianKSFDO BrianKSFDO commented Sep 30, 2017

First method worked, thanks for sharing!

@jayeshdshah

This comment has been minimized.

Copy link

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

Copy link

@xiongmai xiongmai commented Oct 18, 2017

yay thanks! I used the first method.

@Rob-van-B

This comment has been minimized.

Copy link

@Rob-van-B Rob-van-B commented Oct 24, 2017

great!

@TomHaii

This comment has been minimized.

Copy link

@TomHaii TomHaii commented Nov 9, 2017

thanks

@he-dev

This comment has been minimized.

Copy link

@he-dev he-dev commented Nov 19, 2017

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

@jasonmeurer

This comment has been minimized.

Copy link

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

Copy link

@kirkvicent kirkvicent commented Dec 15, 2017

多谢多谢
from china

@Anihilakos

This comment has been minimized.

Copy link

@Anihilakos Anihilakos commented Dec 15, 2017

thank you!

@maxdonetsk

This comment has been minimized.

Copy link

@maxdonetsk maxdonetsk commented Dec 27, 2017

Works like a charm. Thanks!

@Zibri

This comment has been minimized.

Copy link

@Zibri Zibri commented Dec 28, 2017

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

@moshfeu

This comment has been minimized.

Copy link

@moshfeu moshfeu commented Jan 10, 2018

Thanks!

@sebamawa

This comment has been minimized.

Copy link

@sebamawa sebamawa commented Feb 6, 2018

First method ok! . Thanks!

@mohammadmahdikarimi

This comment has been minimized.

Copy link

@mohammadmahdikarimi mohammadmahdikarimi commented Feb 14, 2018

I am completely new and it worked for me. Thanks

@Gabboxl

This comment has been minimized.

Copy link

@Gabboxl Gabboxl commented Apr 24, 2018

Omg, Thanks bro!

@FadeMind

This comment has been minimized.

Copy link

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

Copy link

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

Copy link

@lucasbuetto lucasbuetto commented Jun 20, 2018

Thanks Bro!

@Alexiades

This comment has been minimized.

Copy link

@Alexiades Alexiades commented Jul 2, 2018

Thank you!! first method works perfect!

@greg-amato

This comment has been minimized.

Copy link

@greg-amato greg-amato commented Jul 26, 2018

Thanks!

@indramahkota

This comment has been minimized.

Copy link

@indramahkota indramahkota commented Jul 31, 2018

thanks

@vserrao

This comment has been minimized.

Copy link

@vserrao vserrao commented Aug 9, 2018

Thanks!

@algenisromero

This comment has been minimized.

Copy link

@algenisromero algenisromero commented Aug 12, 2018

Thanks!

@arharo

This comment has been minimized.

Copy link

@arharo arharo commented Aug 21, 2018

Thanks mate!

@angeldev20

This comment has been minimized.

Copy link

@angeldev20 angeldev20 commented Sep 3, 2018

Thanks.

@bernardowb

This comment has been minimized.

Copy link

@bernardowb bernardowb commented Sep 4, 2018

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

@MarkusL27

This comment has been minimized.

Copy link

@MarkusL27 MarkusL27 commented Sep 23, 2018

Thanks! Works perfect!

@Daltonic

This comment has been minimized.

Copy link

@Daltonic Daltonic commented Sep 23, 2018

Thanks Man

@ShinekaBarnett

This comment has been minimized.

Copy link

@ShinekaBarnett ShinekaBarnett commented Oct 17, 2018

Thanks!

@DianaLauraSaavedra

This comment has been minimized.

Copy link

@DianaLauraSaavedra DianaLauraSaavedra commented Oct 18, 2018

donde se hace todo ese metodo

@AndrijReally

This comment has been minimized.

Copy link

@AndrijReally AndrijReally commented Oct 21, 2018

Thanks you!

@WiliTest

This comment has been minimized.

Copy link

@WiliTest WiliTest commented Oct 21, 2018

thanks you but please correct the second method:

And delete the .git folder:

git rm -rf .git

rm -rf .git

(for windows: at the begining create a new folder, open a git bash inside: through windows context menu)

@EricWu23

This comment has been minimized.

Copy link

@EricWu23 EricWu23 commented Nov 2, 2018

Brilliant! Saved my life!

@jgwerner

This comment has been minimized.

Copy link

@jgwerner jgwerner commented Nov 28, 2018

Awesome thanks dude 👍

@phoenixproject

This comment has been minimized.

Copy link

@phoenixproject phoenixproject commented Nov 30, 2018

thanks. Very thanks!

@srinath654321

This comment has been minimized.

Copy link

@srinath654321 srinath654321 commented Dec 17, 2018

Thank you so much the first method worked out well.

@danchamorro

This comment has been minimized.

Copy link

@danchamorro danchamorro commented Jan 3, 2019

Awesome!

@crosslife

This comment has been minimized.

Copy link

@crosslife crosslife commented Jan 13, 2019

Thank you!

@numoonchld

This comment has been minimized.

Copy link

@numoonchld numoonchld commented Jan 16, 2019

thanks! - the first method worked - didn't need to try the second

@renatodysouza

This comment has been minimized.

Copy link

@renatodysouza renatodysouza commented Jan 17, 2019

thank you, so much...

@Sofia190

This comment has been minimized.

Copy link

@Sofia190 Sofia190 commented Jan 26, 2019

Thank you! It's brilliant!

@DanielDimanov

This comment has been minimized.

Copy link

@DanielDimanov DanielDimanov commented Jan 27, 2019

Thank you!

@EPiC-APOC

This comment has been minimized.

Copy link

@EPiC-APOC EPiC-APOC commented Feb 15, 2019

this works for my head repository (origin) how can I pull (or whatever) this to my base repository (upstream)?

@begueradj

This comment has been minimized.

Copy link

@begueradj begueradj commented Mar 13, 2019

None of these methods worked for me.

@deppeler

This comment has been minimized.

Copy link

@deppeler deppeler commented Apr 15, 2019

Just what I was looking for. Thank you for posting and sharing.

@MPGAJEWSKI

This comment has been minimized.

Copy link

@MPGAJEWSKI MPGAJEWSKI commented Jul 21, 2019

The first method worked. Thanks!

@guyueshui

This comment has been minimized.

Copy link

@guyueshui guyueshui commented Aug 28, 2019

I would say: mark! And thank you very much, I've wrote down this method to my blog, thank you!

@widoz

This comment has been minimized.

Copy link

@widoz widoz commented Nov 9, 2019

👍

@holitics

This comment has been minimized.

Copy link

@holitics holitics commented Nov 19, 2019

First method worked for me!

@oseiasmribeiro

This comment has been minimized.

Copy link

@oseiasmribeiro oseiasmribeiro commented Nov 23, 2019

thanks

@Wujin1992

This comment has been minimized.

Copy link

@Wujin1992 Wujin1992 commented Dec 7, 2019

Awesome!

@nasmdh

This comment has been minimized.

Copy link

@nasmdh nasmdh commented Dec 11, 2019

The first method worked for me, tnx.

@actionanand

This comment has been minimized.

Copy link

@actionanand actionanand commented Jan 17, 2020

awesome. Thanks for the update

@gyan111

This comment has been minimized.

Copy link

@gyan111 gyan111 commented Jan 19, 2020

Everyone remember your all commits still accessible by checking the Environment.

Of course for you only.

@skrantzman

This comment has been minimized.

Copy link

@skrantzman skrantzman commented Jan 23, 2020

I really appreciate you posting this. An immense help in clearing up the unneeded clutter. The first method worked perfectly.

@ameyaKetkar

This comment has been minimized.

Copy link

@ameyaKetkar ameyaKetkar commented Jan 28, 2020

Awesome! Thanks a lot.

@roberto-pg

This comment has been minimized.

Copy link

@roberto-pg roberto-pg commented Jan 28, 2020

This is very useful, thanks.

@ambrosektal

This comment has been minimized.

Copy link

@ambrosektal ambrosektal commented Feb 19, 2020

Thank you

@G-Buen0

This comment has been minimized.

Copy link

@G-Buen0 G-Buen0 commented Feb 27, 2020

Thank you !!

@jeffinshadow

This comment has been minimized.

Copy link

@jeffinshadow jeffinshadow commented Feb 29, 2020

Thanks man

@akchauhan2

This comment has been minimized.

Copy link

@akchauhan2 akchauhan2 commented Mar 12, 2020

Leaving the latest commit, i want to delete all the others. How can we do that?

@chhaileng

This comment has been minimized.

Copy link

@chhaileng chhaileng commented Mar 25, 2020

Great, thank you!!

@JonnyRodriguez

This comment has been minimized.

Copy link

@JonnyRodriguez JonnyRodriguez commented Apr 18, 2020

thanks

@igalM

This comment has been minimized.

Copy link

@igalM igalM commented Apr 23, 2020

Great job! Thanks!

@theproayush

This comment has been minimized.

Copy link

@theproayush theproayush commented May 17, 2020

Thank you very much ! the second method worked :)

@ralitsavoronevska

This comment has been minimized.

Copy link

@ralitsavoronevska ralitsavoronevska commented May 29, 2020

Thank you :)

@TomonoriSoejima

This comment has been minimized.

Copy link

@TomonoriSoejima TomonoriSoejima commented Jun 4, 2020

Thank you!

@corestackdev

This comment has been minimized.

Copy link

@corestackdev corestackdev commented Jun 11, 2020

The old commits are still accessible if you have github actions enabled, the action page displays all previous commits and workflow executions. Anyone can still gain access to those previous commits clicking the commit id.

@Dionid

This comment has been minimized.

Copy link

@Dionid Dionid commented Jun 27, 2020

Thanks!

@JayeshThamke

This comment has been minimized.

Copy link

@JayeshThamke JayeshThamke commented Jun 30, 2020

Hi i also have branches on my repo. and want to ensure that those will remain intact and only commits will be deleted.

@halmogbl

This comment has been minimized.

Copy link

@halmogbl halmogbl commented Jul 17, 2020

thank you

@sergiocarp10

This comment has been minimized.

Copy link

@sergiocarp10 sergiocarp10 commented Jul 20, 2020

Thank you so much!

@TheSpanishBread

This comment has been minimized.

Copy link

@TheSpanishBread TheSpanishBread commented Aug 8, 2020

Thank you !!! :D

@miksica

This comment has been minimized.

Copy link

@miksica miksica commented Aug 12, 2020

yer a wizard harry, thanks

@Hiestaa

This comment has been minimized.

Copy link

@Hiestaa Hiestaa commented Aug 17, 2020

If using 2FA for authentication with Github, the HTTPs remote won't be possible. You need SSH instead. Replace git remote add origin https://github.com/heiswayi/myproject.git with git remote add origin git@github.com:heiswayi/myproject.git

@sidharrth2002

This comment has been minimized.

Copy link

@sidharrth2002 sidharrth2002 commented Sep 7, 2020

Method 1 worked like a charm

@suyash0103

This comment has been minimized.

Copy link

@suyash0103 suyash0103 commented Sep 15, 2020

Method 1 worked! Thanks a lot!

@alfonmga

This comment has been minimized.

Copy link

@alfonmga alfonmga commented Sep 25, 2020

An engineer is a man who can do for a dime what any fool can do for a dollar

@Navneet-Suresh

This comment has been minimized.

Copy link

@Navneet-Suresh Navneet-Suresh commented Sep 27, 2020

Method 1 works fine on my Gitea test instance thanks!

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.