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:
|
|
This creates a new branch called newBranch with no commit history.
-
Add and Commit Your Current Files:
1 2git add -A # Add all files and changes git commit -m "Initial commit"This stages and commits all your current files to the new branch.
-
Delete the Old Master Branch:
1git branch -D masterThis deletes the old
masterbranch. -
Rename the Current Branch to Master:
1git branch -m newBranch masterThis renames your current branch (
newBranch) tomaster. -
Force Push the New Master Branch to GitHub:
1git push -f origin masterThis force-pushes the new
masterbranch to GitHub, replacing the old one. -
Optimize Repository Size:
1git gc --aggressive --prune=allThis 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.