Luke
Luke

Reputation: 8407

How to automatically merge consecutively branches after a common parent changed

Hello fellow stack overflow driven developers,

when trying to keep MR small, one approach is to create a branch for small features which always base on the previos small branch. so let's create branch A, commit some stuff, then create branch B, there commit some stuff, then branch C from here and commit there some other stuff. After some comments on your branch A merge request, you would like to update A, but also B and C because they all build upon that one.

So for example something like this

                     B4 --- o
                    /
              B3---
             /
        B2---
       / 
---B1---o---o---o

then might result into a scenario like that

                 B4---o---o
                /        /
              B3-----o--
             /      /
        B2--------o
       /         /
---B1---o---o---o

So the question would be, if there is a git command or some kind of automation which does that all at once.

Update: Currently what I would be Doing is iterate over the array of [B1, B2, B3, B4] and do something like

pseudo code

for (i=0;i<ar.length;i++) {
   if (ar[i-1]) {  
      git checkout ar[i] && git merge ar[i-1]
    }
}

Upvotes: 0

Views: 99

Answers (1)

FoxEcho
FoxEcho

Reputation: 106

Not built in, but you can use a Bash script like this:

#!/bin/bash
trap "exit 1" ERR

while [ $# -gt 1 ]; do
    git checkout $2
    git merge $1
    shift
done

When called with your branches as params (e.g. ./git-merge-consecutive.sh B1 B2 B3 B4), the script will perform the following sequence of git commands:

git checkout B2
git merge B1
git checkout B3
git merge B2
git checkout B4
git merge B3

Note that due to trap "exit 1" ERR, the script will terminate if any command fails.

Upvotes: 1

Related Questions