Preserve some gitignored files while running git clean -X

334 views Asked by At

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?

2

There are 2 answers

4
scharette On BEST ANSWER

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
0
VonC On

From git clean, only -x allows -e files to be considered.
Meaning .project would be deleted either way (-X with ignore rules or -x -e)

if the -x option is specified, ignored files are also removed.
-e <pattern>: In addition to those found in .gitignore (per directory) and $GIT_DIR/info/exclude, also consider these patterns to be in the set of the ignore rules in effect.
-X: Remove only files ignored by Git

As commented, the documentation is confusing for -e.