# git replace: Usage, Options & Common CI Errors

> git replace substitutes one object for another without rewriting history. Reference for --edit, -d, --graft, and why replacements do not push by default.

Source: https://latchkey.dev/learn/command-reference/git-replace  
Updated: 2026-06-25

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

| 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.

## 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
```

> `git rev-parse --abbrev-ref HEAD` returns the literal string `HEAD` on a detached checkout rather than a branch name. On GitHub Actions read `github.ref_name` instead; the git command cannot know what it was checked out for.

## FAQ

### 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.

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
