1

I have a .gitignore file which contains the target directory. And now I have realised that git that git will not delete a git repository which is in the target directory even if I do:

$ git clean -fdx
Skipping repository target/x

Does there exist a solution to handle that? Special configuration option? Cause I have given -f force etc. ?

Update:

I can create manually a repository in the target directory via the following manual steps:

maven-it-extension (issue-21)$ git st
On branch issue-21
Your branch is up to date with 'origin/issue-21'.

nothing to commit, working tree clean
maven-it-extension (issue-21)$ mkdir target/x
mkdir: target: No such file or directory
maven-it-extension (issue-21)$ mkdir -p target/x
maven-it-extension (issue-21)$ cd target/x/
x (issue-21)$ git init .
Initialized empty Git repository in /Users/khmarbaise/ws-git-apache/maven-it-extension/target/x/.git/
x (master #)$ cd ..
target (issue-21)$ cd ..
maven-it-extension (issue-21)$ git status
On branch issue-21
Your branch is up to date with 'origin/issue-21'.

nothing to commit, working tree clean
maven-it-extension (issue-21)$ git clean -fdx 
Skipping repository target/x
maven-it-extension (issue-21)$ 

Update

Using a supplemental -f is the solution which looks like this:

maven-it-extension (issue-21)$ git clean -f -fdx 
Removing target/
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • You have a git repository inside a git repository, are you talking about submodules? – Lasse V. Karlsen Apr 30 '20 at 16:31
  • No submodules. The directory `target` is built by my build tool (Maven) and an automatic release. – khmarbaise Apr 30 '20 at 16:31
  • So it isn't a git repository in there. OK, but if your git repository is already tracking files in there, adding them to gitignore won't make git stop tracking them, you will actually have to remove them from the git repository. – Lasse V. Karlsen Apr 30 '20 at 16:32
  • In the `target` directory there will be created (or more accurate cloned) a git repo during the release process. And those directory `target` has been ignored all the time... – khmarbaise Apr 30 '20 at 16:38
  • The git repo I'm talking about is this: https://github.com/khmarbaise/maven-it-extension there you can take a look...and the `target` directory I'm talking about is at the root level of that repo... – khmarbaise Apr 30 '20 at 16:39
  • See https://stackoverflow.com/questions/9314365/git-clean-is-not-removing-a-submodule-added-to-a-branch-when-switching-branches' – Lasse V. Karlsen Apr 30 '20 at 16:40
  • Unfortunately as I wrote before it's not a submodule/subtree.... – khmarbaise Apr 30 '20 at 16:41
  • But as is said in the answer in that question, if a folder/file is managed by a different repository, git clean ignores it. – Lasse V. Karlsen Apr 30 '20 at 16:43
  • even If I put that whole directory into my `.gitignore` file? That must be change in Git which is missed cause I'm using Git for a long time... – khmarbaise Apr 30 '20 at 16:44
  • That's for you help @LasseV.Karlsen – khmarbaise Apr 30 '20 at 16:47

1 Answers1

7

Try running git clean -f -f -x -d (or git clean -ffxd)

Quoting the doc (and heavoly hinted in the question suggested by @LasseVKarlsen) :

-f, --force

If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f or -i. Git will refuse to modify untracked nested git repositories (directories with a .git subdirectory) unless a second -f is given.

LeGEC
  • 46,477
  • 5
  • 57
  • 104