aljabadi
aljabadi

Reputation: 543

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.

Upvotes: 1

Views: 228

Answers (2)

aljabadi
aljabadi

Reputation: 543

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

git reset -- devlog/*

Upvotes: 1

VonC
VonC

Reputation: 1329542

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/*

Upvotes: 0

Related Questions