{"record":{"id":"fdc58d217cd653e2","repo":"GitoxideLabs/gitoxide","slug":"could-not-retain-state-label-original-state-was","errorCode":null,"errorMessage":"could not retain {state_label}; original state was restored","messagePattern":"could not retain (.+?); original state was restored","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"gix-tix/src/edit/stash.rs","lineNumber":245,"sourceCode":"        .context(\"git stash push did not create refs/stash\")?;\n    let id = stash.peel_to_id()?.detach();\n    if previous == Some(id) {\n        anyhow::bail!(\"git stash push did not create a new stash\");\n    }\n    let target = Target::Object(id);\n    if let Err(err) = repo.edit_references([RefEdit::update(\n        name.clone(),\n        target.clone(),\n        PreviousValue::MustNotExist,\n        reflog_message,\n    )]) {\n        drop(repo);\n        let restore = Command::new(\"git\")\n            .arg(\"-C\")\n            .arg(workdir)\n            .args([\"stash\", \"pop\", \"--index\", \"--quiet\"])\n            .output();\n        return Err(anyhow::anyhow!(err)).context(match restore {\n            Ok(output) if output.status.success() => {\n                format!(\"could not retain {state_label}; original state was restored\")\n            }\n            Ok(output) => format!(\n                \"could not retain {state_label} and git stash pop failed: {}\",\n                output.stderr.trim().to_str_lossy()\n            ),\n            Err(restore) => {\n                format!(\"could not retain {state_label} and could not launch git stash pop: {restore}\")\n            }\n        });\n    }\n    drop(repo);\n\n    let warning = match current(repository_path, bare)? {\n        Some(current) if current == id => {\n            let output = Command::new(\"git\")\n                .arg(\"-C\")","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/GitoxideLabs/gitoxide/blob/e73179060badf27222d790981fac3f84c1830a7e/gix-tix/src/edit/stash.rs#L227-L263","documentation":"In gix-tix `stash save`, when persisting the new stash state fails, the code attempts a recovery via `git stash pop --index` to restore the user's original working-tree state. If the pop succeeds, the primary error is augmented with \"could not retain {state_label}; original state was restored\" — telling the user the operation failed but nothing was lost. If the pop also fails, a harsher message including git's stderr is used.","triggerScenarios":"Any failure of the stash-save operation (error variable `err`) during `save`/`save_manual` while a real worktree is checked out, followed by a successful `git stash pop --index` recovery.","commonSituations":"Disk/permission failures while creating the stash; conflicts preventing the restore path; environments where shelling out to `git` behaves differently than expected.","solutions":["Verify your working tree is back to the original state (git status) before retrying","Fix the root cause from the primary error (permissions, disk space) and retry the save","If state was NOT restored (the alternate message), resolve manually with git stash list/pop"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// confirm the working tree is clean/restored before retrying a stash save\nlet status = Command::new(\"git\").args([\"-C\", workdir, \"status\", \"--porcelain\"]).output()?;\nlet restored = String::from_utf8_lossy(&status.stdout).trim().is_empty();","typeGuard":null,"tryCatchPattern":"match save_state(...) {\n    Err(e) if e.to_string().contains(\"original state was restored\") => {\n        eprintln!(\"save failed but your changes are back in the worktree; safe to retry\");\n    }\n    Err(e) => return Err(e),\n    Ok(v) => v,\n}","preventionTips":["Check `git status` after any failed stash operation","Keep disk space free so stash writes don't fail mid-operation","Prefer library-native stash flows over shelling out to git when possible"],"tags":["git","stash","recovery","state"],"backgroundTag":"git-command-failed","analyzedSha":"e73179060badf27222d790981fac3f84c1830a7e","analyzedAt":"2026-09-08T11:26:50.865Z","contentChangedAt":"2026-09-08T11:26:50.865Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}