In short, here are the steps to make the current commit the only (initial) commit in a Git repository:
- Backup your repository: Ensure you have a backup of your repository’s current state as the following steps cannot be reverted.
- Remove all history and configuration: Execute the following commands:
- Save your
<github-uri>
by runningcat .git/config
. - Delete the
.git
directory usingrm -rf .git
.
- Save your
- Reconstruct the Git repository with the current content:
- If you haven’t set up the
init.defaultBranch
configuration, usegit config --global init.defaultBranch <branch-name>
. For example, setmain
as the<branch-name>
. - Initialize a new Git repository with
git init
. - Add all files to the repository using
git add .
. - Create the initial commit with
git commit -m "Initial commit"
.
- If you haven’t set up the
- Push to GitHub:
- Add the remote origin by executing
git remote add origin <github-uri>
. - Push the changes with
git push -u --force origin main
.
- Add the remote origin by executing
Remember that this method is considered a brute-force approach and should not be used if your repository contains submodules. In such cases, it is recommended to use alternative methods like interactive rebase.
Join the conversation.