githuskygit-huskylint-staged

Why are my changes gone after a cancelled git commit and how do I recover them?


Here's what I did:

  1. Coded 8 hours worth of changes.
  2. git status displays all my changes.
  3. git add -A
  4. git commit -m "Foo". A pre-commit git hook fires with husky and lint-staged.
  5. I remember that there's a TypeScript typing error I failed to fix, so I press Ctrl+C to cancel.
  6. Absentmindedly, I run git commit -m "Foo" again and immediately cancel.
  7. Changes are gone! The files are reverted, git status is clean, git log and git reflog do not show a new commit.

Why did my changes get reverted? How do I recover them?

step 1 step 2


Solution

  • OK, it was lint-staged to blame. It stashed my changes.

    So running git stash apply recovered them!


    ⚠ Do not attempt running multiple instances of lint-staged in parallel on the same git repo (e. g. on each project in a monorepo). This will destroy your changes without any way to recover them.

    If you work on a monorepo, either lint projects sequentially or give up lint-staged and lint the whole codebase, including unstaged files.