Stabledog
Stabledog

Reputation: 3370

Delete or remove all history, commits, and branches from a remote Git repo?

I've read and tried lots of Git command recommendations and discussion, going on over several days now. It appears that there really is no simple, comprehensive way to make a remote Git repo completely empty -- no branches, no refs, no objects, no files, no nothing.

Yes, I recognize that one could delete and recreate the repo -- if one had that kind of permissions on the origin (which I don't), but that is not the point. How is it done? What combination of Git commands will actually do this, leaving the repo in a virgin state ready to receive whatever we wish to push into it, and with essentially no size (or the minimal size of a virgin repo)?

Please don't tell me this shouldn't be done, or that we have to inform all users, etc. I know all that. I just want to start completely fresh.

Upvotes: 22

Views: 16845

Answers (2)

user456814
user456814

Reputation:

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 [email protected]:user/practice.git;

$ git push origin --mirror
remote: error: refusing to delete the current branch: refs/heads/master
To [email protected]:user/practice.git
 ! [remote rejected] master (deletion of the current branch prohibited)
error: failed to push some refs to '[email protected]: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.

Upvotes: 34

iveqy
iveqy

Reputation: 24391

You can't do this. The best you can do is to remove all refs and hope that the server runs git gc and has settings for prune objects that doesn't have any refs. This depends on the server configuration.

Usually it takes 14 days before objects are removed by git gc. However those object won't be cloned if you try to clone the repository.

You've already got a good answer of how to do a "hack" to remove all refs. It works and your repo will appear to you as it is "fresh". However it isn't.

Upvotes: -1

Related Questions