You might want to try pushing an empty local repo with the --mirror
flag (emphasis mine):
--mirror
Instead of naming each ref to push, specifies that all refs under refs/ (which includes but is not limited to refs/heads/
, refs/remotes/
, and refs/tags/
) be mirrored to the remote repository. Newly created local refs will be pushed to the remote end, locally updated refs will be force updated on the remote end, and deleted refs will be removed from the remote end. This is the default if the configuration option remote.<remote>.mirror
is set.
If your repo is on GitHub, you'll get this error if master
is set to the default branch when trying to push:
$ mkdir practice; cd practice;
$ git init; git remote add origin git@github.com:user/practice.git;
$ git push origin --mirror
remote: error: refusing to delete the current branch: refs/heads/master
To git@github.com:user/practice.git
! [remote rejected] master (deletion of the current branch prohibited)
error: failed to push some refs to 'git@github.com:user/practice.git'
I got around this by making an initial commit, then pushing.
Obligatory Warning: this will, of course, completely wipe out all of your history and commits in your remote repo—all references, all branches, all tags, etc. Make sure this is actually what you want to do. Of course, you can always make a backup clone of your remote repo before doing this, in case you want to keep it around for whatever reason.
Also note that none of the commits will actually be deleted right away. They'll just become dangling commits, meaning that they're not reachable from a branch. Eventually they'll get garbage collected by Git repos, but if you have access to your remote repo, you can manually start the garbage collection with git gc
.