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.

@arpanEnbake
Copy link

arpanEnbake commented Feb 26, 2021

thanks for giving commands

@VincentYeh-dev
Copy link

VincentYeh-dev commented Feb 27, 2021

Thanks,first method work perfectly.

@ileuskov
Copy link

ileuskov commented Feb 28, 2021

Thanks a lot!

@morethanbooks
Copy link

morethanbooks commented Mar 5, 2021

Awesome! Thanks!

@calag4n
Copy link

calag4n commented Mar 11, 2021

Thanks :)

@hassandraga
Copy link

hassandraga commented Mar 20, 2021

First method worked, Thank you for sharing this!

@trinib
Copy link

trinib commented Mar 29, 2021

THANK YoU VERY very MUCH ......could not understand on stackflow 😢

@react-hunter
Copy link

react-hunter commented Mar 31, 2021

Hi I have a problem related with commit histories.
I worked on branch - br1. and created pull request.
before merging it, I created new branch - br2 from master.
after that, the PR related br1 has been merged.
by the way, now, the PR related br2 includes all commit histories from the PR related br1.
can I remove only these commit histories? current pr has new commit histories related br2.
I don't want to delete new commit histories.

can you help me?

@trinib
Copy link

trinib commented Apr 1, 2021

Hi I have a problem related with commit histories.
I worked on branch - br1. and created pull request.
before merging it, I created new branch - br2 from master.
after that, the PR related br1 has been merged.
by the way, now, the PR related br2 includes all commit histories from the PR related br1.
can I remove only these commit histories? current pr has new commit histories related br2.
I don't want to delete new commit histories.

can you help me?

i dont know if is this your looking for https://gist.github.com/cutiko/0b1615c63504a940877541362cc51211

@Karthikkadivela
Copy link

Karthikkadivela commented Apr 23, 2021

thanks alot worked with the first one

@jainijhawan
Copy link

jainijhawan commented May 2, 2021

Thanks A lot

Copy link

ghost commented May 3, 2021

Awesome! 😄

@amanarora9848
Copy link

amanarora9848 commented Jul 20, 2021

Worked very well, thank you!

@GaborVxxx
Copy link

GaborVxxx commented Jul 27, 2021

Thank you. Helped me a lot.

@flcoder
Copy link

flcoder commented Jul 31, 2021

Thank you!

@aquawicket
Copy link

aquawicket commented Aug 6, 2021

Thank you. I had a hard time with rebasing 8000+ commits. This worked perfectly

@dassoumyamoy00
Copy link

dassoumyamoy00 commented Oct 26, 2021

Thank you...the first one worked perfectly

@dpirrott
Copy link

dpirrott commented Nov 25, 2021

Thank you! The first method worked perfectly, would never have thought of that on my own.

@toreylittlefield
Copy link

toreylittlefield commented Nov 30, 2021

Thank you! Method 1 worked great and saved my butt!

@dreamlancer
Copy link

dreamlancer commented Dec 26, 2021

Thanks

@khokonm
Copy link

khokonm commented Jan 14, 2022

Hy man, thanks.

@mkgeeky
Copy link

mkgeeky commented Jan 24, 2022

Method 1 works fine 👍

@Galaxy83
Copy link

Galaxy83 commented Mar 16, 2022

thanks

@chauhanh
Copy link

chauhanh commented Apr 28, 2022

First method, worked perfectly! Thank you very much.

@wawan-ikhwan
Copy link

wawan-ikhwan commented Aug 5, 2022

How about delete specific commit history without delete its content by commit id?

@Anandanumasankar
Copy link

Anandanumasankar commented Aug 9, 2022

i dont know where to run these comments could you please help me

@mkgeeky
Copy link

mkgeeky commented Aug 9, 2022

i dont know where to run these comments could you please help me

You can run the commands via ssh

@trinib
Copy link

trinib commented Aug 9, 2022

i dont know where to run these comments could you please help me

Can use git as well https://git-scm.com/downloads

@hrithikram15
Copy link

hrithikram15 commented Oct 3, 2022

### Really Helpful... Thanks a lot

If someone is trying second method & are unable to delete the ".git" folder using command git rm -rf .git (as specified),
I suggest you to just use the command rm -rf .git

It works...

@droodman
Copy link

droodman commented Oct 17, 2022

Just what I needed!

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