Linux command
git-replace 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Replace an object with another object
git replace [object] [replacement]
Force replace an object, overwriting an existing replacement
git replace -f [object] [replacement]
List all replacement refs
git replace --list
List replacement refs matching a pattern
git replace --list 'refs/replace/abc*'
Delete a replacement ref
git replace -d [object]
Edit an object interactively and create a replacement
git replace --edit [object]
Graft a commit to have different parents
git replace --graft [commit] [parent1] [parent2]
Convert legacy grafts file to replacement refs
git replace --convert-graft-file
说明
git replace creates, lists, or deletes refs in the `refs/replace/` namespace that substitute one object for another. This allows changing how objects are viewed without modifying the original objects themselves. Replacements are used by default in all Git commands except reachability traversal (prune, pack transfer, fsck). To bypass replacements, use `git --no-replace-objects` or set the `GIT_NO_REPLACE_OBJECTS` environment variable. Common use cases include fixing published history, grafting histories together, or replacing large blobs. The `--graft` option simplifies rewriting commit parentage, replacing the legacy `$GIT_DIR/info/grafts` mechanism.
参数
- -l _pattern_, --list _pattern_
- List replacement refs. If a pattern is given via glob(7), only matching replacements are listed.
- -d, --delete
- Delete existing replacement refs for the given objects.
- -f, --force
- Overwrite an existing replace ref for the same object instead of failing.
- --graft _commit_ _parent_...
- Create a graft commit. A new commit is created with the same content as the given commit but with the specified parents, then a replacement ref is created to replace the original commit.
- --convert-graft-file
- Convert all entries in `$GIT_DIR/info/grafts` to replace refs and delete the grafts file. This is a one-time migration from the legacy grafts mechanism.
- --edit _object_
- Edit an object's content interactively. The existing content is pretty-printed into a temporary file, an editor is launched, and the result is used to create a replacement object.
- --raw
- When used with `--edit`, provide the raw object contents rather than pretty-printed ones. Useful for editing corrupted objects.
- --format=_format_
- Format for `--list` output: `short` (replaced SHA only), `medium` (replaced -> replacement), or `long` (includes object types). Default: `short`.
FAQ
What is the git-replace command used for?
git replace creates, lists, or deletes refs in the `refs/replace/` namespace that substitute one object for another. This allows changing how objects are viewed without modifying the original objects themselves. Replacements are used by default in all Git commands except reachability traversal (prune, pack transfer, fsck). To bypass replacements, use `git --no-replace-objects` or set the `GIT_NO_REPLACE_OBJECTS` environment variable. Common use cases include fixing published history, grafting histories together, or replacing large blobs. The `--graft` option simplifies rewriting commit parentage, replacing the legacy `$GIT_DIR/info/grafts` mechanism.
How do I run a basic git-replace example?
Run `git replace [object] [replacement]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -l _pattern_, --list _pattern_ do in git-replace?
List replacement refs. If a pattern is given via glob(7), only matching replacements are listed.