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

fra*_*aco 7 git

我试图用git replace改写历史并替换的一个不同的提交的树对象提交.我想把这个永久化.

文档git replace似乎表明这是可能的.我修改了以下配方来替换如何将过去添加到git存储库中的提交?.

# 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
Run Code Online (Sandbox Code Playgroud)

这个工作,master的树已被master2中的一个替换,包括额外的文件:

100644 blob 3b18e512dba79e4c8300dd08aeb37f8e728b8dad readme.txt
100644 blob 16b14f5da9e2fcd6f3f38cc9e584cef2f3c90ebe testfile.txt
Run Code Online (Sandbox Code Playgroud)

但是,尝试使其永久化,失败:

git filter-branch --tag-name-filter cat -- --all
Run Code Online (Sandbox Code Playgroud)

收到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
Run Code Online (Sandbox Code Playgroud)

删除替换引用后,master将其原始树恢复:

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

3b18e512dba79e4c8300dd08aeb37f8e728b8dad readme.txt
Run Code Online (Sandbox Code Playgroud)

为什么要git filter-branch抱怨替换,我该怎么做才能永久替换树?

PS我知道我可以用一个filter-branch --tree-filter或一个移植物来做这个,但是(怎么样)我可以用它做什么git replace

raj*_*aha 0

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

步骤1:进入您要替换的分支。

$ git checkout <your_branch_name>
Run Code Online (Sandbox Code Playgroud)

步骤2:运行以下命令并获取替换提交和替换提交的SHA-1。替换提交 SHA-1 你可以得到顶部的。替换提交 SHA-1 是您想要替换当前分支的选择。

$ git log --status <your_branch_name>
Run Code Online (Sandbox Code Playgroud)

你会得到如下结果:

$ 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
Run Code Online (Sandbox Code Playgroud)

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

$ git replace -f d93378d4e774990160818038eb382f26b29f3c8f f4372c6c5d78eca13aa3017d72dc27f5bd38a08d
Run Code Online (Sandbox Code Playgroud)

第四步:现在使用以下命令提交内容。

$ git commit -am 'getting replaced with commit of "file1.txt"'
Run Code Online (Sandbox Code Playgroud)

步骤 5:现在您可以检查您是否处于替换提交中。