Skip to content

Instantly share code, notes, and snippets.

@heiswayi
Created February 5, 2017 01:32
Show Gist options
  • Select an option

  • Save heiswayi/350e2afda8cece810c0f6116dadbe651 to your computer and use it in GitHub Desktop.

Select an option

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.

@morethanbooks

Copy link
Copy Markdown

Awesome! Thanks!

@calag4n

calag4n commented Mar 11, 2021

Copy link
Copy Markdown

Thanks :)

@hassandraga

Copy link
Copy Markdown

First method worked, Thank you for sharing this!

@trinib

trinib commented Mar 29, 2021

Copy link
Copy Markdown

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

@react-hunter

Copy link
Copy Markdown

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

trinib commented Apr 1, 2021

Copy link
Copy Markdown

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
Copy Markdown

thanks alot worked with the first one

@jainijhawan

Copy link
Copy Markdown

Thanks A lot

ghost commented May 3, 2021

Copy link
Copy Markdown

Awesome! 😄

@amanarora9848

Copy link
Copy Markdown

Worked very well, thank you!

@GaborVxxx

Copy link
Copy Markdown

Thank you. Helped me a lot.

@flcoder

flcoder commented Jul 31, 2021

Copy link
Copy Markdown

Thank you!

@aquawicket

Copy link
Copy Markdown

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

@dassoumyamoy00

Copy link
Copy Markdown

Thank you...the first one worked perfectly

@dpirrott

Copy link
Copy Markdown

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

@toreylittlefield

Copy link
Copy Markdown

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

@khokonm

khokonm commented Jan 14, 2022

Copy link
Copy Markdown

Hy man, thanks.

@mkgeeky

mkgeeky commented Jan 24, 2022

Copy link
Copy Markdown

Method 1 works fine 👍

@Galaxy83

Copy link
Copy Markdown

thanks

@chauhanh

Copy link
Copy Markdown

First method, worked perfectly! Thank you very much.

@ikhwanperwira

Copy link
Copy Markdown

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

@Anandanumasankar

Copy link
Copy Markdown

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

@mkgeeky

mkgeeky commented Aug 9, 2022

Copy link
Copy Markdown

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

You can run the commands via ssh

@trinib

trinib commented Aug 9, 2022

Copy link
Copy Markdown

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
Copy Markdown

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

Just what I needed!

@haiueom

haiueom commented Feb 6, 2023

Copy link
Copy Markdown

Thanks 👍

@topcug

topcug commented Mar 16, 2023

Copy link
Copy Markdown

works like a charm ☺

@skewll

skewll commented May 2, 2023

Copy link
Copy Markdown

Thanks, 1st worked perfectly.

@jameszenartist

Copy link
Copy Markdown

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