Skip to content
Latchkey

git replace: Usage, Options & Common CI Errors

git replace makes Git see one object as if it were another, without touching real history.

Replace objects let you stitch grafted history, test edits to old commits, or join two repositories - all virtually. The original objects stay intact, and replacements live under refs/replace.

What it does

git replace creates a ref under refs/replace that makes Git substitute one object for another at read time. The substitution is local and reversible; the underlying objects are never modified.

Common usage

Terminal
git replace <old-sha> <new-sha>
git replace --graft <commit> <new-parent>
git replace --edit <sha>
git replace -l                 # list replacements
git replace -d <old-sha>       # delete a replacement

Options

FlagWhat it does
--edit <object>Edit an object and replace it with the result
--graft <commit> [<parents>]Rewrite a commit’s parents virtually
-l / --listList existing replacements
-d / --deleteRemove a replacement ref
-f / --forceOverwrite an existing replacement

Common errors in CI

fatal: replace ref ‘refs/replace/<sha>’ already exists - use -f to overwrite or -d to remove first. Replacement refs are not fetched or pushed by default, so a CI job that relies on a graft must fetch refs/replace/* explicitly or set up the replacement itself.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →