How to Make the Current Commit the Only Commit in a Git Repository

  1. Removing the history by recreating the repository
    1. Removing local repository
    2. Reconstructing the repository
    3. Pushing the repository
  2. Removing the history by rebasing
  3. Pushing Repositories
  4. Git Submodule
  5. Interactive Rebase

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 -v

Then, you can delete the local repository by running the next command:

rm -rf .git

Reconstructing the repository

Next step is reconstructing the repository and committing the current state as the initial commit.

git init
git add .
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 master

Removing the history by rebasing

As an alternative, you can use the git rebase command to clear the commit history.

git rebase --root -i

After running this command the editor window will show up all the commits. It will offer you to input the command for each commit. All you need to do is typing "reword" for the first commit and type fixup for all the other commits. After saving a window will open to type the initial commit message. As a result, a new root commit is created, which is a combination of all other commits that came after it.

Pushing Repositories

After the changes are made in the local repository, you can run git push to share them with other members of the team. The git pushcommand is commonly used to publish the upload local changes to the central repository. It is one of the commands that are involved in the "syncing" process. These commands work on the remote branches that are configured with the git remote command.

Git Submodule

Git allows including other git repositories, which are called submodules into one repository. Submodules allow tracking changes in several repositories via one repository. Submodules are repositories that are included in the parent repository at a specific path in the working directory of the parent repository. Submodule supports adding, synchronizing, updating, and cloning submodules.

Interactive Rebase

Interactive rebase is executed with the -i flag short for "interactive". The advantage of interactive rebase mode is that it changes the individual commits in the process, without having to move all the commits to the new base. Due to this mode, you can clean the history by deleting and changing the existing sequence of commits.

Do you find this helpful?

Related articles