以非交互方式查找合并提交的正确父项 [英] Find the right parent of a merge commit in a non-interactive way
问题描述
我正在准备svn2git迁移,而 https://github.com/nirvdrum/svn2git一直非常有用,但我最终还是得到了一些恶作剧.我已经能够清理其中的大多数,但仍然存在.我有一个看起来像这样的历史记录:
A1 - B1 - C1 - E1 - F1
\
A2 - B2 - C2 - D1
A1-C1和A2-C2相同,除了附加的子目录级别. A2-C2实际上是svn迁移和以前的git-filter-branch
操作的产物,我对此进行了确认.
D1是被标记为release_2_0_4
的合并提交.
我想摆脱A2-C2.我看到了两种方法:
- 取消标记D1,标记C1,标记日志过期并进行垃圾回收. of! A2-C2不见了.
- 我还可以重写D1的父母,以便它只有C1作为父母,并带有
git-filter-branch
魔法.
我可以使用第一种方法,因为C1和D1的文件内容相同(用git diff C1 release_2_0_4
检查),但是那样我会丢失提交日期,作者和消息.
第二种方法是:
git filter-branch --tag-name-filter cat --parent-filter 'test $GIT_COMMIT = [sha of release_2_0_4] && echo "-p [sha of FIRST parent of release_2_0_4]" || cat' -- --all
我已经用硬编码的SHA手动进行了尝试,并且可以正常工作.但是我需要编写脚本,而无需了解SHA.
我知道如何找到D1的父母,可以使用脱字符号(release_2_0_4^2
)来引用它们(在这里找到:https://github.com/nirvdrum/svn2git has been incredibly useful, I still ended up with a couple of shenanigans. I've been able to clean up most of them, but one still remains. I have a history that looks like this:
A1 - B1 - C1 - E1 - F1
\
A2 - B2 - C2 - D1
A1-C1 and A2-C2 are identical, except for an additional subdirectory level. A2-C2 is really an artifact of the svn migration and of previous git-filter-branch
operations, I made sure of that.
D1 is a merge commit that was tagged as release_2_0_4
.
I want to get rid of A2-C2. I see two ways of doing that:
- Untag D1, tag C1, reflog expire and garbage collect. Poof! A2-C2 is gone.
- I could also rewrite D1's parents so it only has C1 as a parent, with a bit of
git-filter-branch
magic.
I could use the first method because the file content of C1 and D1 is identical (checked with git diff C1 release_2_0_4
), but then I would lose commit date, author and message.
The second method would be:
git filter-branch --tag-name-filter cat --parent-filter 'test $GIT_COMMIT = [sha of release_2_0_4] && echo "-p [sha of FIRST parent of release_2_0_4]" || cat' -- --all
I already tried it in a manual way with hard coded SHA, and it works. But I need to script it, without knowing the SHA.
I know how to find the parents of D1, they can be referenced with the caret notation: release_2_0_4^1
and release_2_0_4^2
(found that here: Get parents of a merge commit in git), but I am not sure how I can find out which is which. I am doing this in a script, not in an interactive way, and other git-filter-branch
operations may change the SHA hashes, so I need to find a way to determine which of the two commits I need. The only clue I have is that C1 has 2 child commits while C2 has only one child commit.
To summarize, my questions are:
- How can I find
[sha of FIRST parent of release_2_0_4]
? - How do I put this in a Bash script without any confusion with single quotes, double quotes, variable expansion etc?
PS: I don't need to worry about upstream, none of the git repos out there that may contain this code are considered official and they won't be until I give the green light for the svn2git migration.
I found that there is no reliable way to do this, because git sees each parent as an equal, and it just uses them in whatever order it gets them.
So I ended up doing it manually, as described above.
这篇关于以非交互方式查找合并提交的正确父项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!