1

My git project is on a separate ext4 partition, which contains lost+found directory.

It is deleted by git clean -d.

If I try to add it to git, it says

error: pathspec 'lost+found' did not match any file(s) known to git.

If I add it to .gitignore, it is deleted by git clean -X -d

So, how do I say git to leave it alone?

(None of the solutions in Git: Exclude a file with git clean works here)

Igor Liferenko
  • 1,499
  • 1
  • 13
  • 28

1 Answers1

3

If it is an empty folder, try and add a placeholder file instead:

cd /path/to/git/repo
touch lost+found/.gitkeep
git add lost+found/.gitkeep

Then a git clean -d would not delete that folder (since it includes tracked content)

Note: if you are sure you won't have to track anything in that folder, you can replace .gitkeep by a .gitignore (inside the lost+found folder, that you might have to create first with mklost+found, depending on your file system).
The content of that .gitignore would then be '*' (without the simple quotes). Source: torek's comment.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Note that if the directory is deleted, Git will recreate it with `mkdir()` instead of with `mklost+found`. – Dietrich Epp Aug 07 '17 at 04:58
  • @DietrichEpp so one should do a mklost+found first? – VonC Aug 07 '17 at 04:59
  • @VonC I don't know about modern Linux but "back in the day" when the BSD `fsck` needed to put files into `lost+found`, it needed the directory to have a bunch of empty slots, so that there were already known-good (because all checked by that point) disk blocks available so that the directory did not have to be expanded in size, in order to add entries to it. That's what `mklost+found` did. BSD's UFS/UFS2 `fsck` is now much fancier and can create and grow the `lost+found` directory if needed. ZFS doesn't need it, being very different in design. – torek Aug 07 '17 at 14:50
  • @torek OK. Not sure what is the OP environment here, but I'll keep hat in mind. – VonC Aug 07 '17 at 14:52
  • On an unrelated side note, I like to use a possibly-empty `.gitignore` rather than a `.gitkeep`. Here, I'd use a `.gitignore` containing `*`. – torek Aug 07 '17 at 14:52
  • @torek I initially chose gitkeep because I wasn't sure the OP wanted to eventually track files in that folder or not. – VonC Aug 07 '17 at 14:56
  • @torek Still, I have included your suggestion (about `.gitignore`) in the answer for more visibility. – VonC Aug 07 '17 at 14:59