Search code examples
gitgit-clean

Preserve some gitignored files while running git clean -X


I'm working with a repository where you often want to do a clean build. This causes the build script to issue a git clean -Xdff which will remove all files that match patterns in .gitignore. I want some files that are ignored by Git (e.g. .project from Eclipse) to stick around after the clean.

I've tried adding the -e flag for "exclude" but that doesn't seem to help given that we're purposely cleaning the things that Git ignores.

To see for yourself:

~$ mkdir testrepo && cd testrepo
~/testrepo$ git init
Initialized empty Git repository in /home/turbulencetoo/testrepo/.git/
~/testrepo$ echo ".project" > .gitignore
~/testrepo$ touch .project
~/testrepo$ git clean -Xn
Would remove .project
~/testrepo$ git clean -Xn -e .project
Would remove .project

What alteration would you recommend to the git clean call (or surrounding code) in my local build script in order to preserve my .project file during a clean build?


Solution

  • Warning this solution as stated by @turbulencetoo

    will have the side effect of deleting other untracked files in your working set (e.g. newSourceFile.c) if you haven't added them yet.


    I think your problem is you're using X instead of x flag, you should be using this instead ,

    git clean -xn -e .project