Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to completely clear git repository, without deleting it

Tags:

git

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.

like image 639
nemo_87 Avatar asked Feb 18 '15 07:02

nemo_87


People also ask

How do I delete everything from my repository?

In order to delete files recursively on Git, you have to use the “git rm” command with the “-r” option for recursive and specify the list of files to be deleted. This is particularly handy when you need to delete an entire directory or a subset of files inside a directory.

How do I clean my remote repository?

In order to clean up remote tracking branches, meaning deleting references to non-existing remote branches, use the “git remote prune” command and specify the remote name. In order to find the name of your current configured remotes, run the “git remote” command with the “-v” option.


2 Answers

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.

like image 136
Chris Maes Avatar answered Oct 14 '22 05:10

Chris Maes


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

  • clone it
  • delete all remote branches: git push origin --delete <branchName> (see "Delete a Git branch both locally and remotely")
  • make a new orphan master branch (see "How can I completely empty the master branch in Git?")

    git branch -D master
    git checkout --orphan master
    
  • make at least one commit and push it.

like image 8
VonC Avatar answered Oct 14 '22 03:10

VonC