Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to completely empty a remote Git repository?

Tags:

git

Let's say I make a mistake in my first push to a remote Git repository. Can I somehow revert this so that the repository is back to it's initial state? I don't have access to the remote machine, so deleting .git directory is not possible.

The problem, as far as I can see is that you can't "unset" the head.

This is what I (or actually a colleague who asked about this) did (I do a local "remote" repo here so anyone can test this):

mkdir git-remote cd git-remote git init --bare cd .. mkdir git-local cd git-local git clone ../git-remote . touch a git add a git commit -m "initial" git push origin master 

And at this point he realised he pushed the wrong stuff to the remote repository.

The only idea I had is the delete everything from his repo && git rm && git push which would still leave the faulty commit there, but the files wouldn't bother a second push.

like image 610
Makis Avatar asked Feb 07 '11 13:02

Makis


People also ask

How do I remove all files from a remote git repository?

In order to delete files recursively on Git, you have to use the “git rm” command with the “-r” option for recursive and specify the list of files to be deleted. This is particularly handy when you need to delete an entire directory or a subset of files inside a directory.


1 Answers

I think you may have an XY problem. You don't actually need to get the remote repository back into its original state in order to start over; you simply need to start over locally, then force-push that to the remote.

# create a new repository that has the initial commit that you want mkdir foo; cd foo; git init; ...; git commit  # set up a remote git remote add origin <url-of-remote> git branch --set-upstream master origin/master  # push your new history git push -f  # delete obsolete remote branches git push origin :deprecated-branch 

The remote repository never returns to a no-commits state, but it ends up where you want it, and that's all that matters!

(Under the hood, the old commits are actually still in the remote repository, but they're left dangling, with no refs pointing to them. They'll be automatically removed when git gc --auto is triggered by a push at some point.)

If you really, really want to empty it for some reason, you can set receive.denyDeleteCurrent in the remote repository, and push-delete all branches, including the current one. I don't see why you actually need to, though!

like image 184
Cascabel Avatar answered Sep 21 '22 05:09

Cascabel