Skip to content
LatchkeyLatchkey home

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.

Using this in CI

CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.

.github/workflows/ci.yml
- uses: actions/checkout@v4
  with:
    fetch-depth: 0   # history, tags, and git describe all need this

- run: |
    git rev-parse --is-shallow-repository   # expect false
    git rev-parse --abbrev-ref HEAD          # prints HEAD when detached

Frequently asked questions

git replace: Usage, Options & Common CI Errors?
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 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

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card