Delete all files and history from remote Git repo without deleting repo itself

Git

Git Problem Overview


I would appreciate it if someone could tell me how I could delete every single file/folder on my git repository without actually deleting the repository itself. I want to delete all history associated with those files as well.

Git Solutions


Solution 1 - Git

As I explain in this answer to Delete or remove all history, commits, and branches from a remote Git repo?, you can also achieve the same thing as Ceilingfish's answer (i.e. delete all references/branches/tags in the remote repo) by doing the following:

  1. Create new empty repo with initial commit:

     mkdir new
     cd new
     echo "This is the README" > README.md
     git init
     git add .
     git commit -m "Add README.md (initial commit)"
    
  2. Add remote repo as origin:

     git remote add origin <url-to-remote>
    
  3. Mirror push to remote:

     git push origin --mirror
    

That will delete all references/branches/tags in your remote repo, and any dangling commits will probably be garbage collected eventually. From the official Linux Kernel Git documentation for git push (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.

Solution 2 - Git

You can delete a branch from a repository remote like this

git push origin :branchname

if you've got any tags you can delete them like this:

git push origin :refs/tags/tagname

This is assuming you have a remote set up to github called origin

This will leave the local tags / branches on your computer, though.

Solution 3 - Git

This is what I have done

> git rm -r -f -q

> git commit

> git push

Then you have to manually delete the files, git rm remove the files from the repo but not from the file system

Solution 4 - Git

Simple solution

  1. Create an empty repo with git init in a new dir.
  2. git remote add origin <url-to-remote>
  3. If the remote server is github: git checkout -b main
  4. git push origin --mirror

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
Questionmax_View Question on Stackoverflow
Solution 1 - Gituser456814View Answer on Stackoverflow
Solution 2 - GitCeilingfishView Answer on Stackoverflow
Solution 3 - GitChristopheView Answer on Stackoverflow
Solution 4 - GitMasih JahangiriView Answer on Stackoverflow