Removing Git History Commit

If you want to remove Git commit history and start fresh with a new branch while keeping your current files, you can follow these steps:

1
2
3
4
1. **Create a New Orphan Branch:**

   ```shell
   git checkout --orphan newBranch

This creates a new branch called newBranch with no commit history.

  1. Add and Commit Your Current Files:

    1
    2
    
    git add -A  # Add all files and changes
    git commit -m "Initial commit"

    This stages and commits all your current files to the new branch.

  2. Delete the Old Master Branch:

    1
    
    git branch -D master

    This deletes the old master branch.

  3. Rename the Current Branch to Master:

    1
    
    git branch -m newBranch master

    This renames your current branch (newBranch) to master.

  4. Force Push the New Master Branch to GitHub:

    1
    
    git push -f origin master

    This force-pushes the new master branch to GitHub, replacing the old one.

  5. Optimize Repository Size:

    1
    
    git gc --aggressive --prune=all

    This command optimizes the Git repository by cleaning up unnecessary files and history.


Please be cautious when using `git push -f` as it can rewrite the history on the remote repository. Make sure you understand the implications, especially if others are collaborating on the same repository.
0%