Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux command to delete all files except .git folder?

Tags:

git

linux

I want to delete all of the current directory's content except for the .git/ folder before I copy the new files into the branch.

What's the linux command for that?

like image 489
Eric Steinborn Avatar asked Mar 12 '14 00:03

Eric Steinborn


People also ask

How do I delete all files except one?

Using find command In first command, the find command itself deletes the file, using -delete option. In the next 2 commands, the find command passes its output to xargs command which constructs separate rm command for those files. Here is a simple command to find and delete all files in your folder except . zip files.

What happens if I delete .git folder?

Deleting the . git folder does not delete the other files in that folder which is part of the git repository. However, the folder will no longer be under versioning control.


5 Answers

Resetting the index is cheap, so

git rm -rf .
git clean -fxd

Then you can reset the index (with git reset) or go straight on to checking out a new branch.

like image 98
CB Bailey Avatar answered Oct 02 '22 03:10

CB Bailey


With find and prune option.

find . -path ./.git -prune -o -exec rm -rf {} \; 2> /dev/null


Edit: For two directories .git and dist
find . -path ./.git -prune -o \( \! -path ./dist \) -exec rm -rf {} \; 2> /dev/null
like image 31
P̲̳x͓L̳ Avatar answered Oct 02 '22 03:10

P̲̳x͓L̳


As Crayon mentioned in the comments, the easy solution would be to just move .git out of the directory, delete everything, and then move it back in. But if you want to do it the fancy way, find has got your back:

find -not -path "./.git/*" -not -name ".git" | grep git
find -not -path "./.git/*" -not -name ".git" -delete

The first line I put in there because with find, I always want to double-check to make sure it's finding what I think it is, before running the -delete.

Edit: Added -not -name ".git", which keeps it from trying to delete the .git directory, and suppresses the errors. Depending on the order find tries to delete things, it may fail on non-empty directories.

like image 25
cincodenada Avatar answered Oct 02 '22 03:10

cincodenada


One way is to use rm -rf *, which will delete all files from the folder except the dotfiles and dotfolders like .git. You can then delete the dotfiles and dotfolders one by one, so that you don't miss out on important dotfiles like .gitignore, .gitattributes later.

Another approach would be to move your .git folder out of the directory and then going back and deleting all the contents of the folder and moving the .git folder back.

mv .git/ ../
cd ..
rm -rf folder/*
mv .git/ folder/
cd folder
like image 36
Anshul Goyal Avatar answered Oct 02 '22 03:10

Anshul Goyal


for i in `ls | grep -v ".git"` ; do rm -rf $i; done; rm .gitignore;

the additional rm at the end will remove the special .gitignore. Take that off if you do need the file.

like image 42
JeffH Avatar answered Oct 02 '22 02:10

JeffH