How do I ask git to merge branch back to parent

左心房为你撑大大i 提交于 2019-12-06 07:23:02
VonC

Such an alias would involve first finding the "parent" branch.

It isn't trivial, and the question "Find the parent branch of a branch" has some good solutions.

Git’s history is based on a DAG of commits. Branches (and “refs” in general) are just transient labels that point to specific commits in the continually growing commit DAG.
As such, the relationship between branches can vary over time, but the relationship between commits does not.

Once you have the name of the parent branch, then you can use it in your alias.

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/:.*//' 

What it does:

  • For each branch finds merge-base with the current branch
  • Finds branch:log-length pair maximizing log-length
  • Extracts branch name

After that one just need to checkout to it and merge previous head into it:

git checkout $parent_branch && git merge -

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!