How to restart your GIT repository

Posted on:December 23, 2023 at 09:42

If for some reason you want to restart your git repostory (delete all old commits), this is “tutorial” for you. This will create a fresh starting point in your Git repository while preserving the current state of your project (files and content), but discarding all previous commit history.

Steps

  1. Create a New Orphan Branch:

    git checkout --orphan new_old

    This command creates a new branch named new_old which is not based on any existing branch, effectively having no commit history. After running this command, you’ll be on the new_old branch.

  2. Stage All Changes:

    git add -A

    This command stages all changes in your project (new, modified, and deleted files). It prepares all your current project files to be committed as a new initial commit on the new_old branch.

  3. Commit the Changes:

    git commit -m "Initial commit"

    This creates a new commit in the new_old branch with the message “Initial commit”. This commit will act as the new starting point for your project history.

  4. Switch Back to Main Branch:

    git checkout main

    Switches your working directory back to the main branch. The main branch still contains the old commit history at this point.

  5. Replace Main Branch History:

    git reset --hard new_old

    This command resets the main branch to match the state of the new_old branch. All the commit history in main is replaced with the single commit from new_old.

  6. Delete the Temporary Orphan Branch:

    git branch -D new_old

    Deletes the new_old branch as it’s no longer needed. The main branch now contains the current state of your project with a clean history.

  7. Force Push to Remote Repository:

    git push origin main --force

    Forcefully updates the main branch on the remote repository. Warning: This step rewrites history on the remote. It’s a potentially destructive operation and can impact collaborators.

Important Notes