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
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 replacementOptions
| Flag | What it does |
|---|---|
| --edit <object> | Edit an object and replace it with the result |
| --graft <commit> [<parents>] | Rewrite a commit’s parents virtually |
| -l / --list | List existing replacements |
| -d / --delete | Remove a replacement ref |
| -f / --force | Overwrite 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.