I'm trying to implement the Git hook code in the following thread (last post): Git: How to re-stage the staged files in a pre-commit hook . I place the following code in my post-commit hook git stash pop -q
and after it has been executed, all my files in my project are marked for deletion(!) and I need to revert all the files to get back to normal state. Except for this, everything works fine, I'm able to modify (in the pre-commit hook) the file that was already staged for commit and the commit itself is successful and afterwards it contains what I want it to contain. What could I be doing wrong? I'm a Git-beginner so please use simple terminology.
Asked
Active
Viewed 81 times
0

arnold_w
- 59
- 9
-
`git stash pop` means *run `git stash apply` (with the same flags), then, if it says it succeeded, run `git stash drop` (on the same stash selected for apply)*. The `-q` flag just makes the operation quiet so this is applying, then dropping-on-success, the default stash, named `stash@{0}` or just `stash`. If everything is marked for deletion, that suggests that what was in the stash was an empty tree with a parent that matches your current tree, so that the difference between the parent of the stash and the stashed work-tree was "delete all files". – torek Nov 13 '19 at 21:30
-
1(Without more information, it's difficult to guess anything more.) – torek Nov 13 '19 at 21:31
-
So you're suspecting I create the stash incorrectly? I use exactly the code from https://stackoverflow.com/questions/26886363/git-how-to-re-stage-the-staged-files-in-a-pre-commit-hook , that is, `git stash save -q --keep-index "current wd"` to create the stash. – arnold_w Nov 13 '19 at 21:37
-
Possibly. Or perhaps `git stash save --keep-index` *didn't* create a stash, which is one of its behaviors; in that case you'd be popping an unrelated stash. It's difficult to script well with `git stash`. See all the answers to https://stackoverflow.com/q/20479794/1256452 as well. – torek Nov 13 '19 at 22:03
-
If I run the `git stash save -q --keep-index "current wd"` command manually from `C:\Program Files\Git\bin\sh.exe` and afterwards use TortoiseGit to inspect the newly created stash, then it seems to be just fine. If I compare the stash with my working tree I see the expected changes so the creation of the stash seems to be successful. – arnold_w Nov 14 '19 at 15:06
-
Hm, other options include using `git commit --only` or `git commit -a`, which switch Git from using the (regular) index to using a temporary index, which might break `git stash` and/or a later apply in interesting ways. – torek Nov 14 '19 at 15:34