49

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?

4
  • 4
    In most cases I'd just rm -rf * which would skip all dot-files, then manually rm any leftovers as needed. To be particularly fancy you can use find but it's also possible to just ls -A > /tmp/doit then edit /tmp/doit to rm or rm-r everything except .git, and that's often easier. Commented Mar 12, 2014 at 0:55
  • 5
    or just move .git to a diff folder temporary then remove all and then move it back Commented Mar 12, 2014 at 0:56
  • If you need this operation right after cloning, then you can simply clone with the -n (--no-checkout) option: git clone -n -b <branch> <repository> <directory>. Commented Jan 9, 2019 at 9:55
  • Almost duplicate question, but for any file/directory: unix.stackexchange.com/questions/153862/… Commented Jun 21, 2021 at 1:41

7 Answers 7

93

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Nice! This is a good git-specific solution I wasn't aware of, which may well be a better one in this case.
To reset the index, neither git reset nor git checkout worked for me. I have to use git reset --hard.
Neat use of the -x option to bypass the gitignore
Would adding git reset --hard as first command here have any benefit?
if we need to remove tracker files: git update-ref -d refs/heads/
20

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 

10 Comments

doesn't remove any folders
now it works, but it gives me errors saying find: './scss': no such file or directory but still deletes everything. weird
PxL, If you can suppress errors that I'm getting about no such file or directory This is my answer of choice.
@EricSteinborn: added stderr redirection to a null file
Hmmm... 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 executing rm?
|
15

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.

5 Comments

Does this require the first line to run?
Answered my own question, no.
So this works just fine, I feel that the non quoted version was more widely accepted as the correct answer.
The 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 a find -delete, you should run it without the -delete first, to make sure you're not doing something unintended.
Good answer. When replacing -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.
11

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 

1 Comment

I do think rm -r * is the best solution. Keeping .gitignore and other files like that can be important.
1
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.

4 Comments

-1 The user wants to delete all files except the .git files, while those are the same ones you seem to be deleting.
@mu無 They used grep -v, which inverts the match. But it's still a very awkward solution, when there are much better options.
@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 |'`
@TimWolla You're right, but life would be better if those special characters could not/were not used in file names
1

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 ... 

Comments

-4

should find all the files and directories that with the name .git

find . -name .git 

should find all the file and directories not named .git

find . -not -name .git 

delete all the files that you find

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

4 Comments

Try 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.
that is why the command i gave to delete the files has the '-not' in front of it. i'll review my answer.
My 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.
yeah, luckily It's a git repo and I could reclone after testing this out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.