githookpre-commit-hookunstage

git hook to batch unstage all files in a path


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.


Solution

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

    git reset -- devlog/*