gpt4 book ai didi

git - 保留 --no-ff merge 提交与 git filter-branch --subdirectory-filter

转载 作者:太空狗 更新时间:2023-10-29 13:03:16 25 4
gpt4 key购买 nike

我有一个具有以下结构的 git 存储库:

.git/
project-root/
abc/
xyz/

这就是我想要的:

project-root/
.git
abc/
xyz/

我一直在研究如何实现这一目标,这是我目前所发现的:http://git-scm.com/docs/git-filter-branch

据此,我应该使用以下命令实现我想要的:

git filter-branch --subdirectory-filter project-root -- --all

这重写了提交历史,但大多数非快进 merge 提交都丢失了(见下图)。

有什么方法可以保留这些提交吗?

enter image description here

最佳答案

正如文档指出的那样 here :

--subdirectory-filter <directory>

Only look at the history which touches the given subdirectory. The result will
contain that directory (and only that) as its project root. Implies
[Remap_to_ancestor].

我认为问题在于 merge 提交实际上并没有触及给定的子目录,这意味着子目录过滤器甚至不查看它们,因此无法保留它们。因此,我们必须使用另一个过滤器来检查每个提交。 tree-filter 非常适合这个,但是我们必须编写一个脚本来对每个提交执行我们想要完成的操作。

此外,我的问题实际上比示例更广泛,因为我的 project-root 文件夹有我想删除的 sibling 。子目录过滤器会删除它们,但为了使用树过滤器执行此操作,find 可以帮助解决问题。

将每次要运行的命令放在一个单独的文件中很方便。

对于这样结构的 repo :

.git/ 
someDirectory/
someFile.txt
otherDirectory/
dontDeleteThisOne/
project-root/
xyz/
abc/

这对我有用:

git filter-branch --tree-filter /absolute/path/to/filterScript.sh --tag-name-filter cat -- --all 

其中/absolute/path/to/filterScript.sh 是包含以下内容的可执行脚本:

#!/bin/bash

#save the folder dontDeleteThisOne so it won't get deleted
git mv -fk dontDeleteThisOne project-root && \
#remove everything not under project-root
find . -maxdepth 1 -mindepth 1 '(' -name project-root -o -name '.git' ')' -prune -o -exec git rm -rf --ignore-unmatch '{}' ';' && \
#move the contents of the project-root directory up to root
git mv -fk project-root/{,.[!.],..?}* .;

生成的 repo 结构是这样的:

.git/
dontDeleteThisOne/
xyz/
abc/

此结果等同于 git filter-branch --subdirectory-filter project-root 的结果,EXCEPT 根据需要, merge 提交保留在历史记录中.

当然,这比使用子目录过滤器花费的时间要长得多...

关于git - 保留 --no-ff merge 提交与 git filter-branch --subdirectory-filter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18564678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com