Fix: /undo destructively interacts with git staging (#8214) (#8303)

Fixes #8214 by removing the '--staged' flag from the undo git restore
command. This ensures that while the working tree is reverted to the
snapshot state, the user's staged changes (index) are preserved,
preventing data loss. Also adds a regression test.
This commit is contained in:
GalaxyDetective
2025-12-20 02:07:41 +08:00
committed by GitHub
parent b15b5082c6
commit 014235f533
2 changed files with 68 additions and 3 deletions

View File

@@ -469,15 +469,18 @@ fn restore_to_commit_inner(
repo_prefix: Option<&Path>,
commit_id: &str,
) -> Result<(), GitToolingError> {
// `git restore` resets both the index and working tree to the snapshot commit.
// `git restore` resets the working tree to the snapshot commit.
// We intentionally avoid --staged to preserve user's staged changes.
// While this might leave some Codex-staged changes in the index (if Codex ran `git add`),
// it prevents data loss for users who use the index as a save point.
// Data safety > cleanliness.
// Example:
// git restore --source <commit> --worktree --staged -- <prefix>
// git restore --source <commit> --worktree -- <prefix>
let mut restore_args = vec![
OsString::from("restore"),
OsString::from("--source"),
OsString::from(commit_id),
OsString::from("--worktree"),
OsString::from("--staged"),
OsString::from("--"),
];
if let Some(prefix) = repo_prefix {