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?
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.
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.
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.
With find and prune option.
find . -path ./.git -prune -o -exec rm -rf {} \; 2> /dev/null
.git
and dist
find . -path ./.git -prune -o \( \! -path ./dist \) -exec rm -rf {} \; 2> /dev/null
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With