gitbutlerapp/gitbutler · error
stale pre-commit index backup at
Error message
stale pre-commit index backup at '{}'; restore it to '{}' before retrying What it means
Before swapping the index, pre_commit_with_tree backs it up; a leftover backup file from a previous crashed/failed hook means the index on disk may be a swapped copy. The API refuses to overwrite the stale backup and asks the user to restore it to the real index before retrying.
Solutions
- Restore the backup over the index as the message says: copy/move the backup file back to .git/index, then retry.
- If the current index is known-good, delete the stale backup file and retry.
- Re-run `git reset --mixed` (or `git status` to verify) to re-sync the index with HEAD if state is unclear.
Example fix
// before $ but commit ... # error: stale pre-commit index backup // after $ cp .git/gitbutler-index-backup .git/index # restore, path per message $ rm .git/gitbutler-index-backup $ but commit ...
Defensive patterns
Strategy: validation
Validate before calling
let backup = index_path.with_extension("gitbutler-index-backup");
if backup.exists() {
std::fs::copy(&backup, &index_path)?; // restore first
std::fs::remove_file(&backup)?;
} Type guard
fn has_stale_index_backup(index_path: &Path) -> bool {
index_path.with_extension("gitbutler-index-backup").exists()
} Try / catch
match result {
Err(e) if e.to_string().contains("stale pre-commit index backup") => {
restore_backup_then_retry();
}
other => other,
} Prevention
- Restore or delete leftover index backups after crashes before committing.
- Ensure hooks always restore the index in cleanup paths (drop guards).
- Monitor for killed hook processes during long commits.
When it happens
Trigger: Calling pre_commit_with_tree when symlink_metadata(backup_path) succeeds — i.e. the pre-commit index backup file exists from a previous hook run that failed to restore the index (crash, kill, panic mid-hook).
Common situations: App or machine crashed during a commit; a hook was killed mid-run; a prior restore failure (pre_commit_propagates_index_restore_failure) left the backup behind.
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- another pre-commit hook is already using the repository…
- Invalid stage ' ' for
- Snapshot tree needs a 'HEAD' entry if it has only a…
- Unconflicted stage for
- askpass broker must be initialized
AI-assisted analysis of gitbutlerapp/gitbutler@58e5313667 (2026-09-18).
Data as JSON: /api/errors/39c36c75a108d5a8.
Report an issue: GitHub.
Appendix: source
Thrown at crates/gitbutler-repo/src/hooks.rs:108
// lets the restore be a single atomic rename that also keeps the permissions.
let index_path = repo
.index()?
.path()
.context("repository index has no backing file")?
.to_owned();
let backup_path = index_path.with_extension("gitbutler-hook-backup");
let backup_tmp_path = index_path.with_extension("gitbutler-hook-backup.tmp");
let mut transaction_lock =
but_core::sync::LockFile::open(index_path.with_extension("gitbutler-hook-lock"))
.context("failed to open pre-commit index lock")?;
if !transaction_lock
.try_lock()
.context("failed to lock the index for a pre-commit hook")?
{
anyhow::bail!("another pre-commit hook is already using the repository index");
}
match std::fs::symlink_metadata(&backup_path) {
Ok(_) => anyhow::bail!(
"stale pre-commit index backup at '{}'; restore it to '{}' before retrying",
backup_path.display(),
index_path.display()
),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
Err(err) => return Err(err).context("failed to inspect pre-commit index backup"),
}
match std::fs::remove_file(&backup_tmp_path) {
Ok(()) => {}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
Err(err) => return Err(err).context("failed to remove stale temporary index backup"),
}
let had_index = match std::fs::copy(&index_path, &backup_tmp_path) {
Ok(_) => {
std::fs::rename(&backup_tmp_path, &backup_path)
.context("failed to finalize pre-commit index backup")?;
true
}View on GitHub (pinned to 58e5313667)