Mastering Git Repository Initialization: Simplifying the Process to Begin with Your Current Commit

In short, here are the steps to make the current commit the only (initial) commit in a Git repository:

  1. Backup your repository: Ensure you have a backup of your repository’s current state as the following steps cannot be reverted.
  2. Remove all history and configuration: Execute the following commands:
    • Save your <github-uri> by running cat .git/config.
    • Delete the .git directory using rm -rf .git.
  3. Reconstruct the Git repository with the current content:
    • If you haven’t set up the init.defaultBranch configuration, use git config --global init.defaultBranch <branch-name>. For example, set main 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".
  4. 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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *