Skip to content

On failure of a pre-commit hook, run git stash pop #762

Description

@Kenyon-Prater-Baller

I'm getting an error in the pre-commit hook run (I don't have any pre-commit hooks, but that's another issue). When the program finishes running, files have been stashed (as is mentioned in the README) but are not restored.

kenyon@Kenyons-MacBook-Pro ~b % git status
On branch BBS-3445-b
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   db/schema.rb

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   .git_hooks/post_commit/run_annotate_models.rb
	modified:   .overcommit.yml
kenyon@Kenyons-MacBook-Pro ~b % git commit       
Unable to setup environment for pre-commit hook run:
STDOUT:
STDERR:Signature of configuration file has changed!
Run `overcommit --sign` once you've verified the configuration changes.
For more information, see https://github.com/sds/overcommit#security
kenyon@Kenyons-MacBook-Pro ~b % git status
On branch dev
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   file1.txt

kenyon@Kenyons-MacBook-Pro ~b % git stash pop
On branch dev
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   file1.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   file2.txt
	modified:   file3.txt

Activity

  1. bwilde-castle commented on Sep 15, 2026

    @bwilde-castle

    Same data loss on Windows (0.73.0), different trigger: if another process has one of your modified files locked, the git reset --hard in cleanup can't delete it, cleanup fails, and the stash is left behind.

    error: unable to unlink old 'app/models/user.rb': Invalid argument
    fatal: Could not reset index file to revision 'HEAD'.
    

    Popping on failure doesn't fix this trigger — I tried it. The pop hits the same locked file, restores the other files, skips that one, and keeps the stash. So you get a half-restored tree instead of a lost one.

    Also, it's kind of confusing since nothing tells you a stash exists. The error doesn't mention it, and the next commit succeeds with a clean git status, so the changes just look gone. Putting the stash ref in that error message would at least let the user know the work is recoverable.

    As an FYI for anyone hitting this: to recover stashed work, it's better not to use git stash pop. That conflicts, because the stash contains the changes you've since committed. Use git stash branch, which starts from the commit the stash was made on and restores the staged/unstaged split intact:

    git stash branch recover-work 'stash@{0}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions