45

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.
    – torek
    Mar 12, 2014 at 0:55
  • 5
    or just move .git to a diff folder temporary then remove all and then move it back 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>. Jan 9, 2019 at 9:55
  • Almost duplicate question, but for any file/directory: unix.stackexchange.com/questions/153862/… Jun 21, 2021 at 1:41

7 Answers 7

84

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.

5
  • 2
    Nice! This is a good git-specific solution I wasn't aware of, which may well be a better one in this case. Mar 12, 2014 at 23:06
  • To reset the index, neither git reset nor git checkout worked for me. I have to use git reset --hard. May 17, 2018 at 15:37
  • 1
    Neat use of the -x option to bypass the gitignore
    – Andry
    Apr 11, 2019 at 21:42
  • Would adding git reset --hard as first command here have any benefit? Jun 20, 2021 at 22:10
  • if we need to remove tracker files: git update-ref -d refs/heads/
    – nextloop
    Sep 12, 2022 at 4:45
19

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
  • 1
    doesn't remove any folders Mar 12, 2014 at 1:15
  • now it works, but it gives me errors saying find: './scss': no such file or directory but still deletes everything. weird Mar 12, 2014 at 1:23
  • PxL, If you can suppress errors that I'm getting about no such file or directory This is my answer of choice. Mar 12, 2014 at 1:31
  • 1
    @EricSteinborn: added stderr redirection to a null file Mar 12, 2014 at 1:46
  • 1
    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?
    – Antonio
    Mar 12, 2014 at 9:56
14

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
  • Does this require the first line to run? Mar 12, 2014 at 1:25
  • Answered my own question, no. Mar 12, 2014 at 1:27
  • So this works just fine, I feel that the non quoted version was more widely accepted as the correct answer. Mar 12, 2014 at 3:11
  • 1
    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. Mar 17, 2014 at 18:29
  • 1
    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.
    – Daniel F
    Sep 26, 2017 at 19:32
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
  • 1
    I do think rm -r * is the best solution. Keeping .gitignore and other files like that can be important. Mar 12, 2015 at 20:35
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
  • 1
    -1 The user wants to delete all files except the .git files, while those are the same ones you seem to be deleting. Mar 12, 2014 at 1:02
  • 2
    @mu無 They used grep -v, which inverts the match. But it's still a very awkward solution, when there are much better options. Mar 12, 2014 at 1:05
  • @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 |'` Mar 12, 2014 at 1:06
  • @TimWolla You're right, but life would be better if those special characters could not/were not used in file names
    – Antonio
    Mar 12, 2014 at 12:08
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 ...
-5

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
  • 4
    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. Mar 12, 2014 at 1:08
  • that is why the command i gave to delete the files has the '-not' in front of it. i'll review my answer.
    – DannyK
    Mar 12, 2014 at 1:11
  • 2
    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. Mar 12, 2014 at 1:13
  • yeah, luckily It's a git repo and I could reclone after testing this out. Mar 12, 2014 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.