Skip to content

Instantly share code, notes, and snippets.

@heiswayi
Created February 5, 2017 01:32
Show Gist options
  • Save heiswayi/350e2afda8cece810c0f6116dadbe651 to your computer and use it in GitHub Desktop.
Save heiswayi/350e2afda8cece810c0f6116dadbe651 to your computer and use it in GitHub Desktop.
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.

@cmd05
Copy link

cmd05 commented Feb 11, 2021

Thanks brother 👍🏿

@Dr-McFish
Copy link

helfull +1

@arpanEnbake
Copy link

thanks for giving commands

@VincentYeh-dev
Copy link

Thanks,first method work perfectly.

@ileuskov
Copy link

Thanks a lot!

@morethanbooks
Copy link

Awesome! Thanks!

@calag4n
Copy link

calag4n commented Mar 11, 2021

Thanks :)

@hassandraga
Copy link

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

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

thanks alot worked with the first one

@jainijhawan
Copy link

Thanks A lot

Copy link

ghost commented May 3, 2021

Awesome! 😄

@amanarora9848
Copy link

Worked very well, thank you!

@GaborVxxx
Copy link

Thank you. Helped me a lot.

@flcoder
Copy link

flcoder commented Jul 31, 2021

Thank you!

@aquawicket
Copy link

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

@dassoumyamoy00
Copy link

Thank you...the first one worked perfectly

@dpirrott
Copy link

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

@toreylittlefield
Copy link

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

@dreamlancer
Copy link

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

thanks

@chauhanh
Copy link

First method, worked perfectly! Thank you very much.

@ikhwanperwira
Copy link

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

@Anandanumasankar
Copy link

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

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

Just what I needed!

@haiueom
Copy link

haiueom commented Feb 6, 2023

Thanks 👍

@colossus06
Copy link

works like a charm ☺

@skewll
Copy link

skewll commented May 2, 2023

Thanks, 1st worked perfectly.

@jameszenartist
Copy link

Thank you so much for this! The first solution worked great.

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