Linux command to delete all files except .git folder?
Solution 1
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.
Solution 2
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
Solution 3
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.
Solution 4
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
Solution 5
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.

Eric Steinborn
I'm a web designer. I like the internet and I make things that work really well.
Updated on June 13, 2022Comments
-
Eric Steinborn 4 months
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?
-
Anshul Goyal over 8 years-1 The user wants to delete all files except the
.git
files, while those are the same ones you seem to be deleting. -
cincodenada over 8 years@mu無 They used grep -v, which inverts the match. But it's still a very awkward solution, when there are much better options.
-
Anshul Goyal over 8 years@cincodenada The other problem was that this doesn't delete remaining dotfiles within the folder. Also, initially there was some error with the code which has been corrected now which was giving me
-bash: syntax error near unexpected token
|'` -
cincodenada over 8 yearsTry running
find . -not -name .git | grep git
- you'll see that you're trying to delete all the files in the.git
directory, becuase they don't have ".git" in their filenames. You're looking for -path and some wildcards. -
DannyK over 8 yearsthat is why the command i gave to delete the files has the '-not' in front of it. i'll review my answer.
-
cincodenada over 8 yearsMy original comment had a typo where I omitted the -not, which I've corrected. See my corrected comment as to why you need to learn more about how the
find
command works. -
Eric Steinborn over 8 yearsdoesn't remove any folders
-
Eric Steinborn over 8 yearsnow it works, but it gives me errors saying
find: './scss': no such file or directory
but still deletes everything. weird -
Eric Steinborn over 8 yearsDoes this require the first line to run?
-
Eric Steinborn over 8 yearsAnswered my own question, no.
-
Eric Steinborn over 8 yearsPxL, If you can suppress errors that I'm getting about
no such file or directory
This is my answer of choice. -
Eric Steinborn over 8 yearsyeah, luckily It's a git repo and I could reclone after testing this out.
-
P̲̳x͓L̳ over 8 years@EricSteinborn: added stderr redirection to a null file
-
Eric Steinborn over 8 yearsSo this works just fine, I feel that the non quoted version was more widely accepted as the correct answer.
-
Eric Steinborn over 8 yearsI know its a different question than the initial question, but how would you add another ignored folder? Say "dist/" and ".git"?
-
P̲̳x͓L̳ over 8 years@EricSteinborn: added to the answer
-
Antonio over 8 yearsHmmm... I don't think redirecting all possible errors to null is a good idea in general. Why not using the
find
option-delete
instead of executingrm
? -
P̲̳x͓L̳ over 8 years@Antonio: I agree with you regarding redirecting errors. From the docs for
-delete
option:Because -delete implies -depth, you cannot usefully use -prune and -delete together.
-
Antonio over 8 years@TimWolla You're right, but life would be better if those special characters could not/were not used in file names
-
cincodenada over 8 yearsNice! This is a good git-specific solution I wasn't aware of, which may well be a better one in this case.
-
cincodenada over 8 yearsDetailing the difference between this answer and mine:
-prune
effectively limitsfind
to the current directory level, so instead of going through the tree and deleting each file, it usesrm -r
to delete each file or folder in the root directory. And as @P̲̳x͓L̳ noted,-prune
precludes-delete
, since-delete
isn't recursive. -
cincodenada over 8 yearsThe major difference between this and the accepted answer is not the quotes (I have those to protect from shells trying to prematurely expand the glob), but the
-prune
. It's a different way of accomplishing the same task. And as for the first line: any time you are running afind -delete
, you should run it without the-delete
first, to make sure you're not doing something unintended. -
Matthew Mitchell over 7 yearsI do think
rm -r *
is the best solution. Keeping .gitignore and other files like that can be important. -
xRahul over 5 yearsThis is just so convinient!
-
Daniel F about 5 yearsGood answer. When replacing
-path "./.git/*"
with-path "*/.git/*"
then it works for excluding multiple git repositories which are under one common directory. Likeprojects/project1/.git
andprojects/project2/.git
then this can be run directly inside theprojects/
directory. -
Trần Việt Hoàng over 4 yearsTo reset the index, neither
git reset
norgit checkout
worked for me. I have to usegit reset --hard
. -
Andry over 3 yearsNeat use of the
-x
option to bypass the gitignore -
Derek Wright over 2 yearsAdding
-d
to the command will depth sort so dirs are deleted last, then you no longer need the redirect. -
Devin Rhode over 1 yearWould adding
git reset --hard
as first command here have any benefit?