1

I have a devlog folder where I write down my thoughts/example codes which I like to be visible with git status. With the exclusion of these files, quite often I can just git add .; git commit, but obviously the contents in devlog prohibit that. I like to have a git hook which would unstage the whole path and its contents before commit.

I tried:

git rm --cached devlog/*

But apparently, it expects all contents of ./devlog to be staged already (including those ignored by .gitignore) and hence throws an error:

git rm --cached devlog/*
#> fatal: pathspec 'devlog/check.log' did not match any files

Where devlog/check.log is ignored and has not been staged at all.

aljabadi
  • 463
  • 4
  • 16

2 Answers2

1

Here is a solution that seems to work. Simply add to .git/hooks/pre-commit the following:

git reset -- devlog/*
aljabadi
  • 463
  • 4
  • 16
0

You could:

  • force adding check.log first,
  • then git rm --cached everything

Since check.log is ignored (listed in .gitignore), it would be ignored (again) automatically after the git rm --cached.

So:

    git add --force devlog/check.log
    git rm --cached devlog/*
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks. The main issue with this solution is that I will have to update the hook every time I update .gitignore – aljabadi Apr 09 '21 at 07:34
  • @AlJAbadi Why updating the hook multiple times? Only once should be enough. – VonC Apr 09 '21 at 07:41
  • I'm not sure if I understood your proposition correctly. I assume if, for instance, I have `**/*.pdf` in my .gitignore and someday, I start having a pdf file in `./devlog` (or I update my .gitignore to ignore certain file types), I'll have to also `add --force` it in the hook. Please clarify if I'm wrong. – aljabadi Apr 09 '21 at 07:45