Skip to content

Instantly share code, notes, and snippets.

Created February 5, 2017 01:32
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
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.

Copy link

thanks alot worked with the first one

Copy link

Thanks A lot

Copy link

ghost commented May 3, 2021

Awesome! 😄

Copy link

Worked very well, thank you!

Copy link

Thank you. Helped me a lot.

Copy link

flcoder commented Jul 31, 2021

Thank you!

Copy link

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

Copy link

Thank you...the first one worked perfectly

Copy link

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

Copy link

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

Copy link


Copy link

khokonm commented Jan 14, 2022

Hy man, thanks.

Copy link

mkgeeky commented Jan 24, 2022

Method 1 works fine 👍

Copy link


Copy link

First method, worked perfectly! Thank you very much.

Copy link

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

Copy link

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

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

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

Copy link

### 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...

Copy link

Just what I needed!

Copy link

haiueom commented Feb 6, 2023

Thanks 👍

Copy link

works like a charm

Copy link

skewll commented May 2, 2023

Thanks, 1st worked perfectly.

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