Seatext library / BotRefund evidence

Pre-commit Trial Testing: How to Test Changes Before You Commit

Pre-commit trial testing means running your automated checks against the exact changes staged for a commit, not your messy working tree. It catches broken builds and failing tests before they reach CI, saving time...

✓ Built for advertisers who need clear, refund-ready traffic evidence.

Pre-commit Trial Testing: How to Test Changes Before You Commit

What is pre-commit trial testing?

Pre-commit trial testing is the practice of running a focused set of automated checks against the changes you are about to commit. The goal is simple: catch a broken test or a syntax error before it enters your repository history or reaches a shared CI pipeline.

The word "trial" matters here. You are not running your full test suite against whatever happens to be in your working directory. You are testing the exact snapshot that will become the commit. That distinction prevents a common failure mode where a test passes locally because an uncommitted file or a stale build artifact masks a real problem.

Why the working tree is not the commit

When you run tests normally, Git uses your working tree. That tree can contain files you have not staged, files you have modified after staging, and generated files that will never be committed. A pre-commit hook that naively runs pytest or npm test may therefore test code that is not in the commit at all.

Looking to implement this for your website?
Start a free payout audit →

The Stack Overflow discussion on pre-commit hooks highlights this exact trap. The repository might not be clean when the hook runs. If you test the working tree, you can get a green result for code that is not staged, or a red result caused by a file that will not be committed. The fix is to test a clean copy of the staged content, often by using git stash --keep-index or by exporting the staged files to a temporary directory.

How a pre-commit trial test works

A reliable pre-commit trial test follows a small pipeline. First, capture the staged changes. Second, create an isolated environment that contains only those changes plus the committed baseline. Third, run the relevant checks. Fourth, reject the commit if any check fails.

For a simple Python project, the PDC Center for High Performance Computing tutorial shows the basic pattern. You write a test file, confirm it passes with pytest, then add a Git client-side hook that runs that test before a commit is recorded. If the test fails, the commit is blocked.

For a more robust setup, many teams use the pre-commit framework. It manages hooks, caches environments, and runs only the checks you define. You can combine it with a staged-content export so the hook tests the commit, not the working tree.

Step-by-step: set up a trial test before commit

  1. Stage your changes. Use git add on the files you intend to commit. Leave unrelated edits unstaged.
  2. Create a clean snapshot. Use git stash --keep-index to temporarily remove unstaged changes, or export the index to a temp directory with git checkout-index -a --prefix=/tmp/trial/.
  3. Run the focused checks. Execute the test command that covers the staged files. For a Python project, that might be pytest. For a JavaScript project, it might be npm test or a linter plus a type checker.
  4. Restore the working tree. If you used git stash --keep-index, run git stash pop to bring back your unstaged edits.
  5. Commit or fix. If the checks pass, commit. If they fail, fix the staged files, re-stage, and repeat the trial.

One common mistake is running the full suite every time. That makes pre-commit testing slow enough that developers bypass it with --no-verify. A better approach is to run only the tests affected by the staged files, then let CI run the full suite.

What to test before a commit

The exact checks depend on your stack, but a useful pre-commit trial usually includes three layers. First, fast static checks: syntax, formatting, linting, and type checking. Second, unit tests for the changed modules. Third, a build or compile step if your language needs one.

JetBrains' guide on running tests before commit recommends making sure tests pass before they reach the CI/CD pipeline. The idea is to shift failure detection as early as possible. A failing test caught locally costs seconds. The same failure caught in CI costs a pipeline run, a notification, and a context switch.

Client-side vs. server-side hooks

Pre-commit trial testing usually happens in a client-side hook. That hook runs on your own machine when you run git commit. It can block the commit immediately if a check fails.

Server-side hooks run after you push, on the remote repository. They are useful for enforcing policy across a team, but they are too late for the fast feedback loop that pre-commit testing provides. A good setup uses both: client-side hooks for fast local feedback, and server-side hooks or CI for the authoritative gate.

Common mistakes and how to avoid them

  • Testing the working tree instead of the index. Always test the staged snapshot. Use git stash --keep-index or a temp-directory export.
  • Running slow checks in the hook. Keep the pre-commit trial under a few seconds. Move slow integration tests to CI.
  • Bypassing the hook with --no-verify. If developers routinely skip the hook, the hook is too slow or too noisy. Fix the hook, not the developers.
  • Ignoring generated files. Make sure your trial environment does not include build artifacts that can mask a real failure.
  • Not versioning the hook. Store your hook configuration in the repository so every developer runs the same checks.

When pre-commit trial testing does not apply

Pre-commit testing is not a substitute for CI. It cannot run tests that need a database, external services, or a full build environment. It also cannot catch integration problems that only appear when multiple branches merge. Use it as a fast local filter, not as your only quality gate.

For very large monorepos, a full pre-commit trial may be impractical. In that case, run only the checks for the changed packages and let CI handle the rest. For teams that commit infrequently and push directly to a shared branch, a server-side hook or a required CI check may be more effective than a client-side hook that can be skipped.

Key facts

FactDetail
What it testsThe exact staged snapshot, not the working tree
Where it runsClient-side, before the commit is recorded
Main benefitCatches broken tests and syntax errors before CI
Common trapTesting unstaged or uncommitted files
Best practiceKeep the trial fast; run full suite in CI

Frequently asked questions

Why should I test before committing instead of relying on CI?

Local pre-commit testing gives feedback in seconds. CI gives feedback in minutes or hours. Catching a failure locally avoids a broken pipeline run, a failed build notification, and the context switch of returning to old code.

How do I test only the staged changes in Git?

Use git stash --keep-index to temporarily remove unstaged changes, run your tests, then git stash pop to restore them. Alternatively, export the index to a temporary directory with git checkout-index -a --prefix=/tmp/trial/ and test there.

What is the difference between a pre-commit hook and a pre-push hook?

A pre-commit hook runs when you create a commit locally. A pre-push hook runs when you push commits to a remote. Pre-commit is better for fast local feedback; pre-push can run slightly slower checks before code leaves your machine.

Can I skip a pre-commit hook?

Yes, with git commit --no-verify. That is why the hook should be fast and reliable. If developers skip it routinely, the hook is too slow or too noisy and should be fixed.

What should I do if my pre-commit test fails?

Fix the staged files, re-stage them with git add, and run the trial again. Do not commit with --no-verify unless you have a specific, documented reason.

Does pre-commit testing replace code review?

No. Pre-commit testing checks that code works mechanically. Code review checks that the code is well-designed, maintainable, and aligned with the team's goals. Use both.

Further reading and comparison sources

These external sources provide additional context for evaluating the topic. Their inclusion is not an endorsement.

How BotRefund can help

BotRefund applies a similar pre-commit discipline to your affiliate payouts. Before every monthly billing cycle, it audits each conversion using behavioral telemetry, attribution path reconstruction, and click-to-conversion timing. Finance teams receive clean reports that score every conversion as Approve, Review, Hold, or Reject, so you only pay for traffic that passes the trial.

This works without platform integrations. BotRefund reconstructs affiliate click IDs directly from URL parameters and session telemetry, and it deploys in minutes. The main requirement is that your affiliate links carry the click IDs and session data BotRefund needs to reconstruct the attribution path.

Recommended Solution

Ready to Take the Next Step?

See how our verified tools and services help you put this guide into practice.