简体   繁体   English

使用单个 git 命令将一个分支合并到多个其他分支

[英]Merge one branch into multiple other branches using single git command

I have been looking for a solution through which It's possible to merge one branch into multiple other branches with single git command, but couldn't find an answer.我一直在寻找一种解决方案,通过它可以使用单个 git 命令将一个分支合并到多个其他分支中,但找不到答案。

Why I want to do it and What I want to do it?为什么我想这样做,我想做什么?

Is: I have a single Base branch that contains all base code and that will be merged into multiple other branches as:是:我有一个包含所有base codeBase分支,并将合并到多个其他分支中:

base branch: main-code基础分支: main-code

other branches: website-clone-1, website-clone-2, website-clone-3...其他分支: website-clone-1, website-clone-2, website-clone-3...

So actually, I have multiple websites configured with a single repository but on different branches.所以实际上,我有多个网站配置了一个存储库,但在不同的分支上。 So normally, If there is a change that needs to be replicated on all other websites, I will do it firstly on the main-code branch and then would need to check out each of the website branches and then merge theirs.所以通常情况下,如果有需要在所有其他网站上复制的更改,我将首先在main-code分支上进行,然后需要检查每个网站分支,然后合并它们。

So to overcome this repetitive process I'm looking for a solution through which it would be easier for me to merge changes into all other branches with a single line of command instead of doing it on all branches one by one.因此,为了克服这个重复的过程,我正在寻找一种解决方案,通过该解决方案,我可以更轻松地使用一行命令将更改合并到所有其他分支中,而不是一个一个地在所有分支上进行。

Any kind of help will be appreciated.任何形式的帮助将不胜感激。 Cheers!干杯!

I am not aware of any way to do this with a single command but nothing stops you from creating an alias for a simple script like:我不知道有任何方法可以使用单个命令执行此操作,但没有什么能阻止您为简单脚本创建别名,例如:

git for-each-ref --format="%(refname:lstrip=2)" refs/heads/website-clone-* | 
while read branch
do
    git checkout $branch
    git merge -m "your commit message" main-code
done

git-foreach-ref loops over the branches that starts with website-clone-* and returns their abbreviated refname . git-foreach-ref循环遍历以website-clone-*开头的分支并返回它们的缩写refname With lstrip you remove the first n slashes, so make sure you do not insert any additional subpath when naming a branch.使用lstrip可以删除前 n 个斜杠,因此请确保在命名分支时不要插入任何额外的子路径。 The alternative would be to use :short , but it returns the shortest unambiguous refname.另一种方法是使用:short ,但它返回最短的明确引用名。 This means that if you have a tag and a branch with the same name, it will return the complete refname, which would take the checkout to a detached state, without merging the actual branch.这意味着,如果您有一个标签和一个具有相同名称的分支,它将返回完整的 refname,这会将checkout带到分离的 state,而不会合并实际的分支。

The worst part of this script is the fixed commit message, that you cannot extensively customize: the only variables you can use are the branch names that are going to be merged, despite not being very useful for a commit message.该脚本最糟糕的部分是固定的提交消息,您无法对其进行广泛自定义:您可以使用的唯一变量是将要合并的分支名称,尽管对于提交消息不是很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM