如何使用git-replace替换提交树

时间:2014-03-06 14:31:53

标签: git

我正在尝试使用git replace重写历史记录并替换 使用不同提交的树对象提交。我想做到这一点 永久的。

documentation for git replace似乎表明这是 可能。我修改了以下配方来替换How to prepend the past to a 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

这项工作,主人的树已被master2取代,包括 额外的文件:

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

然而,试图使其永久化,失败:

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

收到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

在我删除替换引用之后,master返回其原始树:

git replace -d ${ORIG}
git ls-tree master

3b18e512dba79e4c8300dd08aeb37f8e728b8dad readme.txt

为什么git filter-branch抱怨更换,我该怎么做 更换树永久性?

P.S。我知道我可以用filter-branch --tree-filter或移植物做到这一点,但是 (怎么样)我可以用git replace做什么?

1 个答案:

答案 0 :(得分:1)

建议不要使用$ git replace,但如果有必要,可以按照以下步骤执行此操作。

第1步:转到要替换的分支。

$ git checkout <your_branch_name>

Step2:运行以下命令并获取替换提交的SHA-1并替换commit。替换提交SHA-1,您可以在顶部获取。您可以选择替换提交SHA-1来替换当前分支。

$ git log --status <your_branch_name>

您将得到如下结果:

$ 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

第3步:现在运行以下命令将当前分支替换为替换提交。

$ git replace -f d93378d4e774990160818038eb382f26b29f3c8f f4372c6c5d78eca13aa3017d72dc27f5bd38a08d

步骤4:现在使用以下命令提交内容。

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

Step5:现在您可以检查您是否在替换提交中。