Git "Your local changes would be overwritten" in CI
By Kaveh Alemi·Latchkey
Git stopped a checkout, pull, or merge because the working tree has uncommitted changes that the operation would clobber. On a reused or self-hosted runner this means the previous job left modified or generated files behind.
What this error means
A checkout/pull step fails with error: Your local changes to the following files would be overwritten by checkout and Please commit your changes or stash them before you switch branches. It surfaces on persistent runners that reuse the workspace, not on ephemeral ones.
git checkout output
error: Your local changes to the following files would be overwritten by checkout:
package-lock.json
Please commit your changes or stash them before you switch branches.
Aborting
Diagnose it: depth, refs, or credentials?
CI checkouts are shallow and detached by default, which breaks anything that needs history or a branch name. Before treating it as a credential problem, confirm what the runner actually fetched.
.github/workflows/ci.yml
- run:|git rev-parse --is-shallow-repositorygit rev-parse --abbrev-ref HEAD # prints HEAD when detachedgit log --oneline -3git remote -vgit for-each-ref --format="%(refname)" | head
Common causes
A dirty workspace from a previous job
A reused runner kept build artifacts or generated files (a regenerated lockfile, compiled output) as uncommitted changes, so the next checkout would overwrite them and Git refuses.
Tracked files modified by a build step
A step edited a tracked file in place (formatting, codegen) before a later checkout/pull, leaving the tree dirty.
How to fix it
Reset the workspace before checkout
Discard uncommitted changes and untracked files so the checkout has a clean tree to land in.
Terminal
git reset --hard
git clean -ffdx
git checkout main
Force a clean checkout in the workflow
On GitHub Actions, actions/checkout cleans the workspace by default; ensure it is not disabled.
.github/workflows/ci.yml
- uses:actions/checkout@v4with:clean:true # default; resets the workspace each run
The checkout options that fix most of this
.github/workflows/ci.yml
- uses:actions/checkout@v4with:fetch-depth:0 # full history: diffs, tags, git describesubmodules:recursive # submodules are NOT fetched by defaultpersist-credentials:false # if a later step pushes with its own token
How to prevent it
Use ephemeral runners, or hard-reset + clean at job start on persistent ones.
Avoid committing generated files that build steps rewrite.
Keep actions/checkout clean step enabled (the default).
Frequently asked questions
What causes Git "Your local changes would be overwritten" in CI?
There are 2 common causes: a dirty workspace from a previous job and tracked files modified by a build step. A reused runner kept build artifacts or generated files (a regenerated lockfile, compiled output) as uncommitted changes, so the next checkout would overwrite them and Git refuses.
How do I fix Git "Your local changes would be overwritten" in CI?
There are 2 fixes depending on which cause you have: reset the workspace before checkout and force a clean checkout in the workflow. Work through them in order, since the first is the most common.
What does Git "Your local changes would be overwritten" in CI actually mean?
A checkout/pull step fails with error: Your local changes to the following files would be overwritten by checkout and Please commit your changes or stash them before you switch branches.
How do I stop Git "Your local changes would be overwritten" in CI happening again?
Use ephemeral runners, or hard-reset + clean at job start on persistent ones. The prevention section lists 3 changes that keep it from recurring.