Syed Aqeel
Syed Aqeel

Reputation: 1009

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.

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 branch: main-code

other branches: 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.

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!

Upvotes: 0

Views: 66

Answers (1)

Marco Luzzara
Marco Luzzara

Reputation: 6026

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. With lstrip you remove the first n slashes, so make sure you do not insert any additional subpath when naming a branch. The alternative would be to use :short, but it returns the shortest unambiguous refname. 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.

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.

Upvotes: 2

Related Questions