The 'fatal: Not a valid object name ... for replace' error occurs when Git cannot find the object (commit, tree, or blob) you're trying to replace. This happens when the specified SHA hash doesn't exist in your repository's object database.
When you see the error "fatal: Not a valid object name abc1234 for replace", Git is telling you that the object you're trying to use with the `git replace` command doesn't exist in your repository's object database. The `git replace` command allows you to create replacement references that point to different objects. This is useful for rewriting history without actually changing the commit graph - Git will transparently substitute one object for another whenever it encounters the original. However, both the object being replaced and the replacement object must exist in your repository. This error commonly occurs when you specify an incorrect or abbreviated SHA hash that doesn't match any object, when the object has been garbage collected, or when working with a shallow clone that doesn't contain all the necessary history. The error can also appear if you're trying to replace an object from a remote repository that hasn't been fetched yet.
First, check if the object you're trying to reference actually exists:
# Check if the object exists and what type it is
git cat-file -t abc1234
# If it shows "fatal: Not a valid object name abc1234", the object doesn't exist
# If it shows "commit", "tree", "blob", or "tag", the object exists
# For a commit, you can also try
git log --oneline abc1234 -1
# Check if the full SHA exists (object hashes are 40 characters)
git rev-parse abc1234Abbreviated SHA hashes can be ambiguous. Try using the full 40-character hash:
# If you know the partial SHA, find the full one
git log --all --oneline | grep abc1234
# Or search for objects matching the prefix
git rev-parse abc1234
# Then use the full SHA with git replace
git replace <full-old-sha> <full-new-sha>Note: The minimum abbreviation length is typically 4 characters, but more may be needed if multiple objects share the same prefix.
If you're working with a shallow clone or haven't fetched all history, the object may exist on the remote:
# Fetch all objects from the remote
git fetch --all
# If using a shallow clone, unshallow it
git fetch --unshallow
# Or fetch with full depth
git fetch --depth=999999
# For Git 2.36+, refetch all objects
git fetch --refetchAfter fetching, verify the object is now available:
git cat-file -t abc1234If the object was recently deleted and garbage collected, it might be recoverable:
# Check the reflog for references to the object
git reflog | grep abc1234
# List all dangling objects (objects with no references)
git fsck --lost-found
# Check if the object is in a pack file
git verify-pack -v .git/objects/pack/*.idx | grep abc1234If the object appears in the reflog, you may be able to recover it by creating a branch at that point before the reflog expires:
git branch recovery-branch abc1234If you have another copy of the repository or a backup:
# Clone the repository to a new directory
git clone <repository-url> temp-repo
# Check if the object exists in the fresh clone
cd temp-repo
git cat-file -t abc1234
# If found, copy the object to your original repo
# Objects are stored in .git/objects/<first-2-chars>/<remaining-38-chars>
# For SHA abc1234..., it would be in .git/objects/ab/c1234...Or use git fetch-pack to get specific missing objects:
# Fetch specific missing objects from origin
git fetch-pack origin abc1234Use git fsck to check for repository corruption and missing objects:
# Full repository check
git fsck --full
# Check and list dangling objects
git fsck --lost-found
# Verbose output showing all objects
git fsck --verboseIf fsck reports missing objects, you may need to recover them from a backup or re-clone the repository.
Make sure you're running the command in the correct repository:
# Show the repository root directory
git rev-parse --show-toplevel
# Show the remote URL
git remote -v
# Confirm this is the repository containing the object you're looking for
git log --all --oneline | head -20If you have multiple repositories or worktrees, ensure you're in the right one.
### Understanding git replace
The git replace command creates replacement references stored in .git/refs/replace/. When Git encounters an object referenced by a replacement ref, it transparently uses the replacement object instead. This is useful for:
1. Grafting history: Connecting unrelated repositories by replacing root commits
2. Fixing errors: Replacing commits with corrected versions without rewriting history
3. Hiding information: Replacing commits that contain sensitive data
# List all replacement refs
git replace -l
# Show what an object is replaced with
git replace --format=long
# Delete a replacement ref
git replace -d <sha>### Disabling Replacements
Replacements can cause confusion when debugging. To temporarily ignore them:
# Run a single command without replacements
git --no-replace-objects log
# Or set an environment variable
export GIT_NO_REPLACE_OBJECTS=1
git log### Replacement vs Grafts
The git replace command is the modern replacement for the deprecated grafts mechanism. Old grafts were stored in .git/info/grafts. If you have legacy grafts, convert them:
# Convert grafts to replace refs
git filter-branch --parent-filter 'cat' -- --all### Common git replace Use Cases
Connecting disconnected history:
# Make commit B appear as a child of commit A (connecting histories)
git replace --graft B AReplacing a commit with a fixed version:
# Create a fixed commit
git commit --amend -m "Fixed commit message"
# Get the new SHA
NEW_SHA=$(git rev-parse HEAD)
# Switch back
git reset --hard HEAD@{1}
# Create replacement
git replace OLD_SHA $NEW_SHA### Sharing Replacements
Replacement refs are not fetched or pushed by default. To share them:
# Push replacement refs
git push origin 'refs/replace/*'
# Fetch replacement refs
git fetch origin 'refs/replace/*:refs/replace/*'### Object Types That Can Be Replaced
Any Git object type can be replaced:
- Commits: Replace with another commit
- Trees: Replace with another tree
- Blobs: Replace with another blob
- Tags: Replace with another object
The replacement object must be of the same type as the original (commit for commit, tree for tree, etc.), except that a commit can replace a tag that points to a commit.
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server
fatal: unable to access: Proxy auto-configuration failed
How to fix 'Proxy auto-configuration failed' in Git
fatal: unable to access: Authentication failed (proxy requires basic auth)
How to fix 'Authentication failed (proxy requires basic auth)' in Git
fatal: unable to access: no_proxy configuration not working
How to fix 'no_proxy configuration not working' in Git
fatal: unable to read tree object in treeless clone
How to fix 'unable to read tree object in treeless clone' in Git