nemo_87
nemo_87

Reputation: 4781

How to completely clear git repository, without deleting it

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.

Upvotes: 46

Views: 117691

Answers (3)

Johan Chane
Johan Chane

Reputation: 118

  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
    

Upvotes: 2

Chris Maes
Chris Maes

Reputation: 37712

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.

Upvotes: 62

VonC
VonC

Reputation: 1323115

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

Upvotes: 9

Related Questions