THE GITHTML FIELD GUIDE

Preserve work when Git says local changes would be overwritten

Git stops with “local changes would be overwritten” when an operation would replace tracked working content you have modified. Inspect the named paths and preserve the edits through a suitable commit, scoped stash, or separate copy. Then retry the intended operation. Do not use force or hard reset simply to bypass the protection.

Identify the overlap and the intended destination

The message usually names files whose local edits conflict with a checkout or integration. Those edits may belong to your current task, an unfinished experiment, or another contributor. Read their diffs and check staged changes separately. A tracked file can have both staged and unstaged content, and preserving only one view may miss part of the work.

Also verify the branch or incoming reference you were trying to use. If the destination was wrong, correcting it may avoid the overlap entirely. Do not reorganize valuable edits just to complete a command that was aimed at the wrong branch. The error is a boundary check, not a request to make the working tree empty at any cost.

Choose preservation based on the work

A coherent checkpoint commit is useful when the edits belong on the current branch. A scoped stash can temporarily set aside selected changes; a separate copy is valuable for irreplaceable work or unclear repository state. Inspect the saved result before relying on it. The example commands only inventory the problem paths.

  1. If using a stash, name it and scope it to the intended paths. Check whether new or ignored files also need separate protection.
  2. If the edits actually belong on a new branch, creating that branch from the current commit may preserve the work without first discarding or relocating it.
shell
git status --short
git diff -- path/to/file
git diff --staged -- path/to/file
git branch --show-current

Retry and reconcile intentionally

After preservation, repeat the original operation only if its destination is still correct. Restore or reapply the saved work in the appropriate context and expect possible conflicts if the base changed. Verify the restored content before deleting any temporary copy or stash. A successful switch does not prove that the task’s edits ended up on the right branch, so inspect branch name and status again before committing. Keep unrelated files out of any cleanup performed during this recovery.

  1. Suppose one file contains a staged correction and a later unstaged experiment. Before setting it aside, record which lines belong to each purpose. Restoring the full file can preserve its bytes while losing that staging distinction, so reconstruct the intended commit boundaries before the next commit.
  2. When reapplying saved edits after switching branches, compare the destination’s version before accepting a whole-file replacement. An incoming configuration key may be necessary even though your older copy works locally. Keep the saved version until the merged file has been checked in its new branch context.

Sources and further reading

AI-assisted writing with source-linked guidance and illustrative examples. Read our editorial approach or report a correction.

All fixing git problems guides →

Read as MarkdownAll guides

Preserve work when Git says local changes would be overwritten · gitHtml