Make the current commit the only (initial) commit in a Git repository? Make the current commit the only (initial) commit in a Git repository? git git

Make the current commit the only (initial) commit in a Git repository?


Here's the brute-force approach. It also removes the configuration of the repository.

Note: This does NOT work if the repository has submodules! If you are using submodules, you should use e.g. interactive rebase

Step 1: remove all history (Make sure you have backup, this cannot be reverted)

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

Step 2: reconstruct the Git repo with only the current content

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

Step 3: push to GitHub.

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


The only solution that works for me (and keeps submodules working) is

git checkout --orphan newBranchgit add -A  # Add all files and commit themgit commitgit branch -D master  # Deletes the master branchgit branch -m master  # Rename the current branch to mastergit push -f origin master  # Force push master branch to githubgit gc --aggressive --prune=all     # remove the old files

Deleting .git/ always causes huge issues when I have submodules.Using git rebase --root would somehow cause conflicts for me (and take long since I had a lot of history).


This is my favoured approach:

git branch new_branch_name $(echo "commit message" | git commit-tree HEAD^{tree})

This will create a new branch with one commit that adds everything in HEAD. It doesn't alter anything else, so it's completely safe.