Hot to reset / reinitialise a git repository

It’s quite rare that you’d need to do this and the steps below are not recommended unless you absolutely don’t need anything in the repo you are about to nuke. My use case was I was moving a repo from one account to another and I didn’t want to transfer the repo. So repo A (the one with the correct data) to repo B (the new home for repo A).

Unfortunately, repo B had a few test commits in it from some time back. If I was to migrate the data from repo A into repo B with these commits still in place then git was getting confused and putting the master line from repo A into one of the branches in repo B which isn’t what I wanted.

So here’s how I fixed it:

  1. Checkout the repo you want to reset/clean/reinitialise/wipe/nuke to a local temp directory
  2. Open the git command line in that repo and run:
    git rebase -i --root
  3. This will open a text file with the list of commits
  4. Change “pick” to “drop” on each of the commit lines
  5. Save and close the file – the updates should be made
  6. Run:
    git push origin +master
  7. The changes should now be deleted

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.