Is it possible to completely empty a remote Git repository?

Git

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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!

Solution 2 - Git

Below steps will do it easily, Be careful to take master backup in case needed.

mkdir  reset
cd reset
 git init
 touch README.md
 git add .
 git commit -m 'Initial commit'
 git remote add origin [email protected]:XXX/xxx.git
 git push --force --set-upstream origin master

Solution 3 - Git

When you say "first push" do you mean the very first push to the branch ever? As in there was no code in it at all before you pushed to it. If that's the case…

If you wanted to push a local branch foo to a remote branch for the first time, you would do this:

git push origin foo:foo

In order to delete remote branch foo, you tell git to push "nothing" to it:

git push origin :foo

Now you can start over with it however you like.

To be honest I'm not sure what actually remains physically on the server -- but in terms of completely resetting branch history, this will work.

(fwiw, I think this interface is pretty ridiculous)

Solution 4 - Git

Best way to empty the GitHub repository.

Step 1: Create new branch. git checkout -b master1

Step 2: Add your data into its.

git add .

git commit -m "Initial commit"

git push origin master1

Step 3: Now remove old master branch.

git push origin --delete <your_branch>

Solution 5 - Git

You can git revert commits, e.g. git revert HEAD.

Keep in mind that the content you committed will reside in the .git directory. (You can't do anything about that)

It's impossible to actually delete a commit from the history without having access to the .git directory.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMakisView Question on Stackoverflow
Solution 1 - GitCascabelView Answer on Stackoverflow
Solution 2 - Gitsandeep chouguleView Answer on Stackoverflow
Solution 3 - GitJohn BachirView Answer on Stackoverflow
Solution 4 - GitMayank DudakiyaView Answer on Stackoverflow
Solution 5 - Gitdeleted77View Answer on Stackoverflow