What is git replace?

Why Interviewers Ask This

This is a differentiating question used for senior and lead roles. Interviewers want to see if you can explain not just what happens, but why — and what the trade-offs are in different approaches.

Answer

git replace allows you to create transparent substitutions for Git objects — replacing one commit (or tree/blob) with another without modifying the original. Replacements are stored in refs/replace/ and are applied transparently when you traverse history. Usage: git replace old-commit-sha new-commit-sha. After this, any Git command that encounters the old SHA transparently uses the new one. Listing: git replace -l. Remove: git replace -d old-sha. Replacements are not pushed by default: git push origin refs/replace/* to share them. Use cases: (1) Grafting history: attach a new repo's history to an old repo without rewriting all commits — create a new orphan commit that is equivalent to the old final commit and replace it, then the histories appear connected; (2) Fix an old commit non-destructively for everyone — replace the old commit with a corrected version, push the replacement, others fetch it without needing to reset anything; (3) Reconnect truncated history — when you used shallow clones and now need full history, graft can connect them. Git replace is a relatively obscure feature used for advanced history manipulation scenarios.

Common Mistake

Rushing to answer is a common mistake. Take two seconds to structure your response: definition → example → trade-off. This structure makes complex Git & GitHub answers easy to follow.