0

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!

1
  • Due to how Git works, a single command is not possible because you need to checkout the target branch in case there is a conflict that needs solving by the user in the working directory. So your only solution is writing a script that checkout and merge for each branch. Commented Apr 2, 2021 at 12:58

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

that or have a list of branch names in a file, maintained manually : cat target-branches | while read branch ...
@LeGEC Right, but in that case you probably need to git add that file too. If the branches accepting the merge follow a pattern I would use my version. Otherwise, yours is certainly the most convenient way.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.