60

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.

Braiam
  • 1
  • 11
  • 47
  • 78
max_
  • 24,076
  • 39
  • 122
  • 211
  • 1
    Your title says that you're talking about cleaning out a "remote git repo" and you've tagged this question "github", so are you trying to reduce the amount of space that you are using on github? Or are you talking about another remote repository? A few more details would be helpful here. – Mark Longair Mar 19 '11 at 18:17
  • I would like to 'delete every single file/folder on my git repo without actually deleting the repo itself.'. I am talking about my github repository. – max_ Mar 19 '11 at 18:22
  • 1
    delete with history or keep history intact? – knittl Mar 19 '11 at 18:25
  • 2
    then ceilingfish's answer is correct (delete branches and tags). why do you want to do that? – knittl Mar 19 '11 at 18:38

4 Answers4

79

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.

Community
  • 1
  • 1
11

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.

Ceilingfish
  • 5,397
  • 4
  • 44
  • 71
  • 1
    it will not allow me to delete the master branch. Is there a way where I can just delete all the files within it? – max_ Mar 19 '11 at 18:48
  • 1
    You could create an empty branch locally (`git branch cleanerbranch`, delete all the files in it, then commit that), then push that to the remote `git push origin cleanerbranch:master`). – Ceilingfish Mar 19 '11 at 18:54
  • That would still have the history, though, assuming `git branch cleanerbranch` was run while on `master`. The OP would have to do something like [GitHub's advice for creating a `gh-pages` branch at a new, empty, root commit](http://pages.github.com/#project_pages) and then `git push -f origin gh-pages:master` – Mark Longair Mar 19 '11 at 20:39
  • 7
    @Mark: Or just create a new repository, with an initial commit of whatever it is you want (including nothing, if desired), and push that repo's master branch. Done. – Cascabel Mar 19 '11 at 21:24
  • @Jefromi: yes, much easier :) – Mark Longair Mar 19 '11 at 21:30
  • @Jefromi - I did not get it. How? I have to add remote first? How to I push? – Sergey Romanov Nov 13 '12 at 13:59
  • @Jefromi `git push origin --mirror` is also particularly effective, as explain in [this answer](http://stackoverflow.com/questions/5363857/delete-all-files-and-history-from-remote-git-repo-without-deleting-repo-itself/18177448#18177448). –  Aug 11 '13 at 23:24
  • Does not work `! [remote rejected] master (refusing to delete the current branch: refs/heads/master)` – Green Apr 09 '19 at 14:01
4

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

Christophe
  • 207
  • 3
  • 2
1

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
Masih Jahangiri
  • 9,489
  • 3
  • 45
  • 51