Skip to content

Replace

The git replace command in Git allows you to create replacement objects, which can be used to substitute one Git object with another. This feature is particularly useful for rewriting commit history, testing changes without altering the original objects, and temporarily substituting parts of your repository for debugging or other purposes.

git replace <old-object> <new-object>
  • old-object: The SHA-1 hash of the object you want to replace.
  • new-object: The SHA-1 hash of the replacement object.

Basic Usage

Creating a Replacement

Suppose you have two commit objects, and you want to replace commit1 with commit2.

  1. Identify the SHA-1 hashes of the two commits:

    git log
    
    Let's say commit1 has the SHA-1 abcdef123456... and commit2 has 123456abcdef....

  2. Create the replacement:

    git replace abcdef123456 123456abcdef
    

This command tells Git to use commit2 whenever it encounters commit1.

Listing Replacements

To list all existing replacements, use:

git replace -l

Deleting a Replacement

To delete a replacement, use:

git replace -d <old-object>

For example:

git replace -d abcdef123456

Use Cases for git replace

  1. Testing Changes: You can test changes by creating replacement objects without altering the original repository history.
  2. Rewriting History: Temporarily rewrite parts of your history for demonstrations or experiments.
  3. Debugging: Substitute objects to debug issues in different parts of the repository.

Examples

Example 1: Replacing a Commit

Imagine you have a commit history like this:

A -- B -- C -- D (master)

You want to replace commit C with a new commit C' for testing purposes.

  1. Create a new commit C':

    git checkout -b temp-branch C
    echo "New changes" > file.txt
    git add file.txt
    git commit -m "Commit C'"
    

  2. Identify the SHA-1 hashes of C and C':

    git log
    

Let's say C is ccccc and C' is ddddd.

  1. Replace C with C':

    git replace ccccc ddddd
    

  2. Verify the replacement:

    git log
    

You should see that the history now shows C' instead of C.

Example 2: Replacing a Blob (File Content)

Suppose you have a file file.txt with an object ID blob1, and you want to replace its content with another blob blob2.

  1. Identify the SHA-1 hash of the blobs:

    git hash-object file.txt
    

  2. Create a new blob:

    echo "New content" > new_file.txt
    git hash-object -w new_file.txt
    

Let's say blob1 is bbbbb and blob2 is fffff.

  1. Replace blob1 with blob2:

    git replace bbbbb fffff
    

  2. Verify the replacement:

    git cat-file -p bbbbb
    

This should show the content of blob2 instead of blob1.

Important Notes

  • Temporary Nature: The replacements are temporary and only affect your local repository unless explicitly shared with others.
  • Sharing Replacements: To share the replacements, you need to push the .git/refs/replace/ namespace to the remote repository:

    git push origin refs/replace/*
    

  • Removal of Replacements: When you no longer need the replacement, remove it using the -d flag as shown earlier.

Using git replace, you can flexibly manage and test changes in your repository without permanently altering your commit history or object data.