vscode icon indicating copy to clipboard operation
vscode copied to clipboard

Git: staging a file with unsaved edits silently stages stale on-disk content (commit/stash already prompt to save)

Open sigpanic opened this issue 1 month ago • 2 comments

  • VS Code version: 1.140.0
  • OS version: Linux x64 6.6.87.2-microsoft-standard-WSL2 (WSL2, remote Ubuntu-24.04)

Type: Bug

Summary Clicking the stage (+) button on a file that has unsaved edits runs git add against the file on disk, so the old content gets staged. The editor buffer still holds the new content, leaving the file in MM state (staged old + unstaged new). The user then reviews/commits content that differs from what the editor shows.

Why this is a bug, not a feature request The Git extension already guards against this exact situation for commit and stash, but not for stage:

  • commit — git.promptToSaveFilesBeforeCommit (enum always|staged|never, default always): dialog "…has unsaved changes which won't be included in the commit… Would you like to save it before committing?" → saves, then re-adds.
  • stash — git.promptToSaveFilesBeforeStash (default always): same pattern.
  • stage — no equivalent check at all.

So with default settings, committing a dirty file prompts, but staging the same dirty file silently stages stale content.

Steps to reproduce

  1. Edit a tracked file and do not save (keep the dirty dot).
  2. In Source Control, click the + (Stage Changes) for that file.
  3. Compare what got staged against what the editor shows — they differ.

Aggravating, very common setup (staging lands before the save):

  • files.autoSave: "onFocusChange"
  • While reviewing a diff, edit the current side, then click +.
  • Leaving the editor triggers autosave, but the stage command runs against the pre-save disk content; staging completes first, autosave second → stale content staged, file ends in MM.
  • Two independent async paths (blur→save vs click→git add) with no ordering guarantee.

Expected Reuse the same prompt/behavior as commit and stash, ideally behind a new git.promptToSaveFilesBeforeStage with identical semantics (always|staged|never, default always).

To be explicit: I am not asking for a silent autosave before staging. Silently flushing a dirty buffer to disk is itself a data-loss risk; a prompt is the right shape and is already VS Code's chosen pattern for commit and stash.

Actual Stale on-disk content is staged with no warning; user must notice the MM state and re-stage.

Code path (main, Oct 2026, commit 16023148)

  • extensions/git/src/commands.ts — @command('git.stage') → repository.add(resources)
  • extensions/git/src/repository.ts — add() → this.repository.add(resources.map(r => r.fsPath))
  • extensions/git/src/git.ts — add() → git add -A <paths> (reads from disk)
  • No dirty-document check anywhere on the stage path.
  • Contrast: commit guard at commands.ts (uses git.promptToSaveFilesBeforeCommit), stash guard at commands.ts (uses git.promptToSaveFilesBeforeStash).
  • Also affects stageAll / stageFile / stageSelectedChanges, which all funnel into repository.add.

Proposed fix Factor out the existing dirty-document prompt (already used by commit/stash) into a shared helper and call it from the stage entry points, controlled by git.promptToSaveFilesBeforeStage (default always), matching commit/stash.

Related

  • #190315, #104945, #114363, #107452 — history of the commit-side prompt.

sigpanic avatar Oct 05 '26 12:10 sigpanic

This issue really needs to be resolved because it leads the user to believe that when staging, the latest changes have been considered, since they don't appear in "Source Control->Changes" until the file is saved. If the user is then prompted to save the file, they might think that these changes have already been staged and discard them.

BDisp avatar Nov 02 '26 00:11 BDisp

I tried this in VS Code Insiders 1.148 on Linux. I edited a tracked file without saving it and clicked Stage Changes (+) on it in Source Control. No prompt appeared, and the index got the saved content from disk, without the unsaved line, as the clip shows.

Staging runs git add, which reads the file from disk. Commit and stash check for unsaved files first (git.promptToSaveFilesBeforeCommit / git.promptToSaveFilesBeforeStash), but the stage commands have no such check. Until they do, save the file (Ctrl+S) before staging it.

I'm keeping this open as a feature request for a matching save prompt before staging.

https://github.com/user-attachments/assets/79a57de7-5933-4cca-8015-96874fe719cd

🤖 Authored with GitHub Copilot on behalf of @lszomoru.

lszomoru avatar Nov 24 '26 11:11 lszomoru