Skip to content

Instantly share code, notes, and snippets.

@iykex
Last active May 19, 2018 20:28
Show Gist options
  • Save iykex/c26e3157c9617d7da5564f8192a207d5 to your computer and use it in GitHub Desktop.
Save iykex/c26e3157c9617d7da5564f8192a207d5 to your computer and use it in GitHub Desktop.
GitHub </> Deleting commits history with git commands

Method One

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.

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

NOTE: This will not keep our old commits history around.

Method Two

Clone the project, e.g. testproject is my project repository:

  • git clone https://github/username/testproject.git

Since all of the commits history are in the .git folder, we have to remove it:

  • cd testproject

And delete the .git folder:

  • git rm -rf .git

Now, re-initialize the repository:

git init
git remote add origin https://github/username/testproject.git
git remote -v

Creating new branch

  • git branch -m testbranch :: skip here if not parsing ...

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.


Credit

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