How to Make the Current Commit the Only Commit in a Git Repository
In this tutorial, you will get the codes of how to make the current commit as an initial one in one repository. The case of submodules is also discussed.
Sometimes it is necessary to remove the version history from the git repository so that the current repository state becomes the initial commit. Here, we will help you to do that.
Removing the history by recreating the repository
Removing local repository
First of all, back up everything not to lose anything.
Now, get the remote URL by running the following command:
git remote -vThen, you can delete the local repository by running the next command:
rm -rf .gitReconstructing the repository
Next step is reconstructing the <kbd class="highlighted">repository</kbd> and committing the current state as the initial commit.
git init
git add . # Stages all files, including untracked ones
git commit -m "Initial commit"Pushing the repository
The final step left to do is pushing the repository to GitHub (suppose, the name of remote is origin, which is by default) :
git remote add origin <remote-url>
git push -u --force origin mainRemoving the history by rebasing
As an alternative, you can use the git rebase command to clear the commit history.
git rebase --root -iAfter running this command, your editor will open with a list of all commits. Change the first commit to pick (or reword if you want to edit its message) and change all subsequent commits to squash or fixup. After saving, Git will combine them into a single root commit.