How to fix Cannot create a new backup. A previous backup already exists in refs/original/ in Git

GitINTERMEDIATEMEDIUMUpdated Dec 6, 2025ยท 118 views

Git filter-branch refuses to run because backup refs from a previous operation already exist. You can either force overwrite the backup with -f flag or manually delete the refs/original namespace.

Quick fix

Fixes Cannot create a new backup. A previous backup already exists in refs/original/

git filter-branch -f --tree-filter 'your-filter-here' HEAD
  1. Use the -f flag to force overwrite
  2. Delete backup refs using git commands
  3. Remove refs from filesystem directly
  4. Clean up completely after filter-branch
git โ€” error
git filter-branch -f --tree-filter 'your-filter-here' HEAD
Cannot create a new backup. A previous backup already exists in refs/original/

What this error means

This error occurs when you run `git filter-branch` on a repository that has already undergone a filter-branch operation. Git's filter-branch command automatically creates backup references under `refs/original/` before rewriting history, allowing you to recover if something goes wrong. When filter-branch detects existing backup refs, it stops to prevent accidentally overwriting your safety net. This is a protective measure, not a fatal error - Git is asking you to explicitly confirm that you want to proceed. The backup refs contain pointers to your original commit history before the rewrite. Once you verify that your previous filter-branch operation completed successfully and you no longer need the backup, you can safely proceed with the new operation.

Typical symptoms

  • Running git filter-branch fails immediately with 'Cannot create a new backup' message
  • The error message mentions 'refs/original/' directory
  • You can see backup refs when running 'git for-each-ref refs/original/'
  • The .git/refs/original/ directory exists and contains reference files
  • Subsequent filter-branch commands fail even after changing the filter expression

Common causes

  1. A previous git filter-branch operation was run on this repository and completed successfully
  2. A previous filter-branch was interrupted or failed partway through, leaving backup refs behind
  3. You are running filter-branch multiple times to iteratively clean up repository history
  4. The repository was cloned from another repo that had filter-branch run on it
  5. Backup refs were packed into .git/packed-refs and remain after deleting the refs/original directory
  6. Running a script that calls filter-branch multiple times without cleanup between runs

How to fix "Cannot create a new backup. A previous backup already exists in refs/original/"

1Use the -f flag to force overwrite

The simplest solution is to add the -f (force) flag to your filter-branch command. This tells Git to remove the existing backup and create a new one:

bash
git filter-branch -f --tree-filter 'your-filter-here' HEAD

Or for other filter types:

bash
git filter-branch -f --env-filter 'your-filter-here' -- --all

Warning: Using -f will delete your existing backup refs. Make sure you don't need them before proceeding.

2Delete backup refs using git commands

If you want to explicitly remove the backup refs before running filter-branch again, use this command:

bash
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin

This safely removes all refs under refs/original/ using Git's own reference management. Alternatively, you can delete them one by one:

bash
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d

After removing the refs, run your filter-branch command normally.

3Remove refs from filesystem directly

If the git commands don't work (for example, if refs are corrupted), you can delete the backup directory directly:

bash
rm -rf .git/refs/original

Important: If you've run git gc, the refs might be packed. Check and edit .git/packed-refs to remove any lines starting with refs/original/:

bash
# View packed refs containing 'original'
grep refs/original .git/packed-refs

# If found, edit the file and remove those lines
# Then run garbage collection
git gc --prune=now
4Clean up completely after filter-branch

After your filter-branch operation succeeds and you've verified the results, perform a complete cleanup to reclaim disk space:

bash
# Remove backup refs
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin

# Expire all reflogs immediately
git reflog expire --expire=now --all

# Run aggressive garbage collection
git gc --aggressive --prune=now

This removes all traces of the old history and can significantly reduce repository size, especially if you removed large files.

5Verify the cleanup was successful

Confirm that all backup refs have been removed:

bash
# Should return nothing
git for-each-ref refs/original/

# Check the directory doesn't exist
ls .git/refs/original/

# Verify packed-refs has no original entries
grep refs/original .git/packed-refs

If all commands return empty or "not found," you can safely run filter-branch again.

Advanced notes

## Consider Migrating to git-filter-repo

The Git project now recommends using git-filter-repo instead of git filter-branch. The newer tool offers several advantages:

- Performance: Orders of magnitude faster than filter-branch, especially on large repositories
- Safety: Better handling of edge cases and fewer silent corruption risks
- Simplicity: More intuitive command syntax for common operations
- No backup conflicts: Uses a different mechanism that avoids the refs/original issue

### Installation

bash
# Using pip
pip install git-filter-repo

# Or download directly
curl https://raw.githubusercontent.com/newren/git-filter-repo/main/git-filter-repo > git-filter-repo
chmod +x git-filter-repo

### Converting filter-branch commands

The git-filter-repo project provides documentation for converting filter-branch commands. For example:

bash
# Old filter-branch way
git filter-branch --tree-filter 'rm -f passwords.txt' HEAD

# New filter-repo way
git filter-repo --path passwords.txt --invert-paths

### Using filter-lamely for compatibility

If you have scripts that rely on filter-branch syntax, filter-lamely provides a drop-in replacement that uses filter-repo under the hood:

bash
# Instead of:
git filter-branch -f --tree-filter 'rm -f secret.txt' HEAD

# Use:
filter-lamely --tree-filter 'rm -f secret.txt' HEAD

## Recovering from a bad filter-branch

If you need to undo a filter-branch operation, the backup refs are your lifeline:

bash
# Restore master branch to its original state
git checkout refs/original/refs/heads/master -B master

# Or reset to the backup
git reset --hard refs/original/refs/heads/master

This is why Git is cautious about overwriting backup refs - they're your only way to recover if the rewrite goes wrong.

Frequently asked questions

What does "Cannot create a new backup. A previous backup already exists in refs/o" mean?
This error occurs when you run git filter-branch on a repository that has already undergone a filter-branch operation. Git's filter-branch command automatically creates backup references under refs/original/ before rewriting history, allowing you to recover if something goes wrong. When filter-branch detects existing backup refs, it stops to prevent accidentally overwriting your safety net.
How do I fix "Cannot create a new backup. A previous backup already exists in refs/o"?
Git filter-branch refuses to run because backup refs from a previous operation already exist. You can either force overwrite the backup with -f flag or manually delete the refs/original namespace.
What causes "Cannot create a new backup. A previous backup already exists in refs/o"?
A previous git filter-branch operation was run on this repository and completed successfully A previous filter-branch was interrupted or failed partway through, leaving backup refs behind You are running filter-branch multiple times to iteratively clean up repository history
Is the "Cannot create a new backup. A previous backup already exists in refs/o" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading