Skip to content

Instantly share code, notes, and snippets.

@nilforooshan
Forked from heiswayi/repo-reset.md
Last active March 17, 2019 03:10
Show Gist options
  • Save nilforooshan/fcc76b3667d78065a4d8cc9c997f70cc to your computer and use it in GitHub Desktop.
Save nilforooshan/fcc76b3667d78065a4d8cc9c997f70cc to your computer and use it in GitHub Desktop.
git: Delete commits history with git commands

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:

# Clone the project
git clone <REPOSITORY><PROJECT>.git

# Since all of the commits history are in the `.git` folder, we have to remove it:
cd <PROJECT>

# Check out to a temporary branch:
git checkout --orphan TEMP_BRANCH

# Add all the files:
git add -A

# Commit the changes:
git commit -am "First 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
git clone git clone <REPOSITORY><PROJECT>.git

# Since all of the commits history are in the `.git` folder, we have to remove it:
cd <PROJECT>

# And delete the `.git` folder:
rm -rf .git

# Now, re-initialize the repository:
git init
git remote add origin <REPOSITORY><PROJECT>.git
git remote -v

# Add all the files and commit the changes:
git add --all
git commit -am "First commit"

# Force push update to the master branch of our project repository:
git push -f origin master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment