Make the current commit the only (initial) commit in a Git repository?
Solution 1:
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 init
git add .
git commit -m "Initial commit"
Step 3: push to GitHub.
git remote add origin <github-uri>
git push -u --force origin master
Solution 2:
The only solution that works for me (and keeps submodules working) is
git checkout --orphan newBranch
git add -A # Add all files and commit them
git commit
git branch -D master # Deletes the master branch
git branch -m master # Rename the current branch to master
git push -f origin master # Force push master branch to github
git 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).
Solution 3:
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.
Solution 4:
The other option, which could turn out to be a lot of work if you have a lot of commits, is an interactive rebase (assuming your git version is >=1.7.12):git rebase --root -i
When presented with a list of commits in your editor:
- Change "pick" to "reword" for the first commit
- Change "pick" to "fixup" every other commit
Save and close. Git will start rebasing.
At the end you would have a new root commit that is a combination of all the ones that came after it.
The advantage is that you don't have to delete your repository and if you have second thoughts you always have a fallback.
If you really do want to nuke your history, reset master to this commit and delete all other branches.