我如何要求git将分支合并回父级 [英] How do I ask git to merge branch back to parent
问题描述
有没有一种方法可以写一个别名,将一个分支合并回到其父级?我知道我可以做到:
Is there a way to write an alias that merges a branch back to its parent? I know I can do:
git checkout - && git merge -
但是它仅在简单的情况下(当我的前一个分支是父分支时才有效).
据我所知git不会跟踪此类信息,因为那里没有分支.但我希望它已经实现为插件或挂钩集.
预先感谢.
But it only works in simple cases when my previous branch is the parent branch.
As far as I know git does not track this kind of information because there is no branches in there. But I hope it's already implemented as a plugin or a hookset.
Thanks in advance.
推荐答案
正如VonC所说,我首先要做的是找到当前分支的父级.以下脚本主要满足我的需求:
As VonC said first I need to do is to find parent of the current branch. The following script mostly does what I need:
git branch | grep -v '*'|xargs -I{} sh -c ' printf "{}:"; git log --oneline `git merge-base "$(git rev-parse --abbrev-ref HEAD)" "{}"` | wc -l' | sort -t: -k +2n | tail -n 1 | sed 's/:.*//'
它的作用:
- 对于每个分支,找到与当前分支的合并基础
- 查找branch:log-length对,以最大化log-length
- 提取分支名称
在那之后,只需签出并将其合并到其中:
After that one just need to checkout to it and merge previous head into it:
git checkout $parent_branch && git merge -
PS:我没有在大型存储库上对其进行测试,如果存在当前分支的分支,它的行为将令人惊讶.它需要修复.
PS: I didn't test it on large repositories and it can behave suprisingly if exists a branch of the current branch. It needs to be fixed.
这篇关于我如何要求git将分支合并回父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!