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 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.
-
Delete the Old Master Branch:
1
git branch -D master
This deletes the old
master
branch. -
Rename the Current Branch to Master:
1
git branch -m newBranch master
This renames your current branch (
newBranch
) tomaster
. -
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. -
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.