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?
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.
git reset
nor git checkout
worked for me. I have to use git reset --hard
.
May 17, 2018 at 15:37
git reset --hard
as first command here have any benefit?
Jun 20, 2021 at 22:10
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
find: './scss': no such file or directory
but still deletes everything. weird
Mar 12, 2014 at 1:23
no such file or directory
This is my answer of choice.
Mar 12, 2014 at 1:31
find
option -delete
instead of executing rm
?
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.
-prune
. It's a different way of accomplishing the same task. And as for the first line: any time you are running a find -delete
, you should run it without the -delete
first, to make sure you're not doing something unintended.
Mar 17, 2014 at 18:29
-path "./.git/*"
with -path "*/.git/*"
then it works for excluding multiple git repositories which are under one common directory. Like projects/project1/.git
and projects/project2/.git
then this can be run directly inside the projects/
directory.
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
rm -r *
is the best solution. Keeping .gitignore and other files like that can be important.
Mar 12, 2015 at 20:35
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.
.git
files, while those are the same ones you seem to be deleting.
Mar 12, 2014 at 1:02
-bash: syntax error near unexpected token
|'`
Mar 12, 2014 at 1:06
as CB Bailey mention:
I want to remove the history of tracker files
too.
git rm -rf .
git clean -fxd
git update-ref -d refs/heads/master #or main or ...
find . -name .git
find . -not -name .git
find . -not -name .git -exec rm -vf {} \;
be sure that the find is doing what you want
if you want to delete directories change the rm command to rm -rvf
I include the v
option to see the files that are deleted.
if you want to make sure about the files before you delete them pipe the find command to a file and review the results
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.
Mar 12, 2014 at 1:08
find
command works.
Mar 12, 2014 at 1:13
rm -rf *
which would skip all dot-files, then manually rm any leftovers as needed. To be particularly fancy you can usefind
but it's also possible to justls -A > /tmp/doit
then edit/tmp/doit
to rm or rm-r everything except.git
, and that's often easier..git
to a diff folder temporary then remove all and then move it back-n
(--no-checkout
) option:git clone -n -b <branch> <repository> <directory>
.