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

标签 git

我正在尝试使用 git replace 重写历史并替换 tree 使用不同提交的树对象提交。我想做这个 永久性的。

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

这行得通,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 提示替换,我该怎么做 更换永久树?

附言我知道我可以用 filter-branch --tree-filter 或移植来做到这一点,但是 (如何)我可以用 git replace 来做吗?

最佳答案

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

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

$ git checkout <your_branch_name>

Step2: 运行以下命令并获取替换提交的 SHA-1 并替换提交。替换提交 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"'

第 5 步:现在您可以检查您是否正在进行替换提交。

关于git - 如何使用 git-replace 替换提交树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22227459/

相关文章:

git - 使用可视化工具查看git stash

git - IntelliJ - GIT 在 GUI 中删除本地孤立分支

git - 正确推送 git 子模块

git - 什么是正确的 Git p4 工作流程?

python - 在 Git Pull 上安装 -r requirements.txt?

git - 如何从别人的 repo 中 pull 出远程分支

git - 当我 stash 更改时,我如何知道我在哪个分支上?

Gitlab 代理 SSL (Docker) 证书验证失败

git - 'fatal: LF would be replaced by CRLF'会影响源代码吗?

git - Maven Release Plugin 在 Jenkins Pipeline 中的使用