How to use git-place to replace commit tree

I am trying to use git replace to rewrite history and replace the commit tree with a tree object of another commit. I want to make it permanent.

The documentation for git replace seems like this is possible. I adapted the following recipe to replace the commit from How do I add the past to the git repository? .

 # setup a second branch with a different tree # existing repo which already contains a few commits git checkout master -b master2 echo "test file" > testfile.txt # a different tree git add testfile.txt git commit -m "test" # save the SHA1 of the original tree for later reference ORIG=$(git rev-parse master^{tree}) # replace the tree of master with the one from master2 git replace master^{tree} master2^{tree} # verify the contents of the replaced tree git ls-tree master 

this works, the master tree has been replaced with master2, including an additional file:

 100644 blob 3b18e512dba79e4c8300dd08aeb37f8e728b8dad readme.txt 100644 blob 16b14f5da9e2fcd6f3f38cc9e584cef2f3c90ebe testfile.txt 

However, trying to make this permanent, it fails:

 git filter-branch --tag-name-filter cat -- --all 

gives the following answer from git:

 Rewrite e7619af3e5d424a144845c164b284875c4c20c7a (2/2) WARNING: Ref 'refs/heads/master' is unchanged WARNING: Ref 'refs/heads/master2' is unchanged error: Object bc705106db90164b2e688b4b190d4358f8b09a56 is a tree, not a commit error: Object bc705106db90164b2e688b4b190d4358f8b09a56 is a tree, not a commit fatal: ambiguous argument 'refs/replace/e7a75e980c7adc0d5ab1c49ecff082b518bb6cf8^0': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]' WARNING: Ref 'refs/replace/e7a75e980c7adc0d5ab1c49ecff082b518bb6cf8' is unchanged 

and after I remove the replacement link, the master gets its original tree back:

 git replace -d ${ORIG} git ls-tree master 3b18e512dba79e4c8300dd08aeb37f8e728b8dad readme.txt 

Why is git filter-branch complaining about replacing and how do I do this replacing the tree permanently?

PS I know that I can do this with filter-branch --tree-filter or grafts, but (how) can I do this with git replace ?

+7
git
source share
1 answer

Using $ git replace not recommended, but if necessary, you can do this by following these steps.

Step 1: Go to the branch you want to replace.

 $ git checkout <your_branch_name> 

Step 2: Run the following command and get a SHA-1 replacement and replace the commit. Replace the SHA-1 fixation you can get at the top. And replacing the SHA-1 commit of your choice, where you want to replace the current branch.

 $ git log --status <your_branch_name> 

You will get the result:

 $ git log --status master commit d93378d4e774990160818038eb382f26b29f3c8f master Author: rajesh < your@mail.com > Date: Tue Jul 22 19:00:15 2014 +0000 latest commit commit 08eb045297566d38eec5f8c2c2f987ebd7fbc2b9 master Author: rajesh < your@mail.com > Date: Wed Jul 16 06:44:17 2014 +0000 updated file1.txt commit f4372c6c5d78eca13aa3017d72dc27f5bd38a08d master Author: rajesh < your@mail.com > Date: Wed Jul 16 06:40:36 2014 +0000 file1.txt commit 65614018eb46c797a49cedee97653bb7716b16c2 master Author: rajesh < your@mail.com > Date: Wed Jul 16 12:03:55 2014 +0530 Initial commit 

Step 3: Now run the following command to replace the current branch with a commit replacement.

 $ git replace -f d93378d4e774990160818038eb382f26b29f3c8f f4372c6c5d78eca13aa3017d72dc27f5bd38a08d 

Step4: Now pass things using the following command.

 $ git commit -am 'getting replaced with commit of "file1.txt"' 

Step 5: Now you can verify that you are on a commit replacement.

+2
source share

All Articles