37

How can I remove all files and folders that I'm storing on git repo, but keep the repository? Is there some command for restarting git repo? I've found many examples on this, but I've made even bigger mess. The repository is linked. So if someone is willing to explain it to me step by step I would be very thankful.

nemo_87
  • 4,523
  • 16
  • 56
  • 102
  • What for? The whole purpose of version control is to keep track of changes, which is the point of keeping an invalid history? Just wipe out your local copy, create a new one setting its remote to the same as your current one, then `git push -f` to completely overwrite the current repository history which seems useless – ThanksForAllTheFish Feb 18 '15 at 07:56
  • please clarify your question; do you want to keep or delete your files? what about your history? what is the actual git repo status and how do you want it to be? – Chris Maes Feb 18 '15 at 08:01
  • @ChrisMaes Well history at this point is not important to me. It is pretty much new repo, so I can delete both history and files and folders, but clearing files are priority to me... – nemo_87 Feb 18 '15 at 08:07
  • can you comment on the anwsers below if that is what you want or not? – Chris Maes Feb 18 '15 at 08:13
  • @ChrisMaes I got error saying: Updates were rejected because the remote contains work that you do not have locally. This is usually caused by another repository pushing to the same ref. You may want to first integrate the remote changes (e.g., 'git pull ...') before pushing again. What if I don't want to pull it, cause I want to push new version of project? P.S. Error happened when I've tried to do push to remote. – nemo_87 Feb 18 '15 at 08:34
  • ok so you have a remote repository to which you are linked (you might put that in your question; your question lacks a lot of info! please clarify so we can give you a quick answer!) – Chris Maes Feb 18 '15 at 08:53
  • @ChrisMaes Yes I'm linked to repository. Sorry for not putting this in my question, I thought that it doesn't change the way you clear it...I'm gonna edit my question right away. – nemo_87 Feb 18 '15 at 08:58
  • @nemo_87 I extended my answer to include how to do this with a remote repository. Please comment if it is not what you are looking for. – Chris Maes Feb 18 '15 at 09:02
  • Is there a reason why you don't just create a completly new repository? – Sascha Wolf Feb 18 '15 at 12:04

3 Answers3

54

if you only have a local git repository

if you want to erase your whole history and start over again:

cd <repo>
rm -rf .git
git init

and start committing again.

If you want to remove both files and history:

cd <repo>
rm -rf *
git init

and start adding files and committing...

if you are linked with a remote repository if you want to start over again, but don't really mind some old history remaining; there is a quick way:

git pull
git rm -r *
git commit
git push

now your repository is empty again, you'll only have some old history remaining. if you also want to clean up all your history; that takes a little more work (and note that this will cause trouble for anyone else linked to that same remote repository):

git checkout <first commit hash>
git rm -r *
touch README
git add README
git commit --amend
git push -f

note that I create a empty README file for the first commit, since a commit cannot be empty.

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • Alternativly you could create a second commit tree, by using `git checkout --orphan `. Your old history would persist, but won't be touched by your new one. – Sascha Wolf Feb 18 '15 at 12:04
  • 2
    not working. Whenever I move from detached HEAD to master branch all the old stuff comes back. – Craig Hicks Aug 08 '19 at 22:16
  • 1
    Reset seemed to work better for me - https://stackoverflow.com/a/16500248/4376643 – Craig Hicks Aug 08 '19 at 22:26
7

If you are talking about an existing remote repo (and not just a local repo, which is trivial to do), you can:

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for answering :) When I try to delete master branch (which is the only one) I got this error: By default, deleting the current branch is denied, because the next 'git clone' won't result in any file checked out, causing confusion You can set 'receive.denyDeleteCurrent' configuration variable to 'warn' or 'ignore' in the remote repository to allow deleting the current branch, with or without a warning message. To squelch this message, you can set it to 'refuse'. refusing to delete the current branch: refs/heads/master @VonC And I'm not sure am i understand it right way... – nemo_87 Feb 18 '15 at 08:39
  • @nemo_87 it was a local repo only!? that explains your error message. I'll leave the answer for others which actually have to empty a (remote) repo. – VonC Feb 18 '15 at 09:59
  • @nemo_87 for a local repo, you don't have to do what the first answer suggest. Simply `git init` anywhere and you are good to go: your question doesn't make sense for a local repo. – VonC Feb 18 '15 at 10:00
  • Deleting the remote master branch worked fine when I had limited permissions within repo in Azure DevOps, but didn't have permission to delete the whole repo and recreate new. – Jussi Palo Aug 13 '19 at 07:27
  • @JussiPalo That is an issue indeed. I am pretty sure I did not thought of Azure Devops and its privileges when writing this in 2015 (since it was not yet named DevOps at the time: https://en.wikipedia.org/wiki/Microsoft_Visual_Studio#Azure_DevOps_Services) – VonC Aug 13 '19 at 07:32
1
  1. Reset commit or index or worktree

    git reset --hard <commit>       # reset commit, index and worktree
    
  2. Remove the reflogs

    git reflog expire --expire=all --all        # clear reflog
    
  3. Cleanup unnecessary files and optimize the local repository

    git gc --prune=now