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

If you want to remove the version history from the local Git repository so that the current repository content becomes the only commit. This tutorial shows how to make the current commit as an initial one in a single repository. The case of submodules is also discussed in the topic.

Firstly, remove all history but also be sure you have backup because this cannot be reverted:

cat .git/config # note <github-uri>
rm -rf .git

Next step is reconstructing the Git repository with only the current content:

git init
git add .
git commit -m "Initial commit"

The final step left to do is pushing the repository to GitHub:

git remote add origin <github-uri>
git push -u --force origin master

In the case of Git Submodules, an interactive rebase can be used. For example, if your git version is 1.7.12, then run:

git rebase --root -i

In the list of commits in your editor, change "pick" to "reword" for the first commit and "pick" to "fixup" for every other commit. After doing this, Git will start the rebasing process. 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