Commit a change to more than one branch in Git

自古美人都是妖i 提交于 2019-12-05 08:36:11
Tim Henigan

I expect git cherry-pick is what you want.

After committing the fix to the first branch, you can use git cherry-pick to merge it into each of the other branches.

This related question on SO may be of interest: Git & Working on multiple branches

The common approach to this is "merging upwards". From man gitworkflows:

Always commit your fixes to the oldest supported branch that require them. Then (periodically) merge the integration branches upwards into each other.

This gives a very controlled flow of fixes. If you notice that you have applied a fix to e.g. master that is also required in maint, you will need to cherry-pick it (using git-cherry-pick(1)) downwards. This will happen a few times and is nothing to worry about unless you do it very frequently.

The first method is of course preferred - it's good to have a commit in your repo only once, and to be able to see the history of how it got into each branch. Life isn't perfect, though, and you'll sometimes find yourself in the second category. If that situation becomes common enough, you could perhaps write a script like

multi-cherry-pick <commit> <branch> [<branch>...]

which checks out each branch in turn and cherry-picks the given commit.

Jakub Narębski

Yes, there is. Make this commit on separate topic (feature) branch (branching off oldest branch / earliest state), and then merge this topic branch into any branch you want.

This workflow is described for example in Never merging back blog post by Junio C Hamano (maintainer of Git).

That is roughly what Jefromi wrote wrote

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