2

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 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

4 Comments

This does work to save the file, but it 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.
@turbulencetoo Isn't what git clean is for ?
depends on the use case, I guess. In this case the -X was used to make sure that only specific patterns of files get cleaned.
@turbulencetoo added clarification.
2

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.