LoginSignup
4
0

More than 5 years have passed since last update.

developブランチの内容を複数のfeatureブランチにマージするスクリプト

Last updated at Posted at 2017-12-14

developブランチにDB関連や設定ファイルをコミットしたときなど、すべてのfeatureブランチにdevelopブランチの内容を取り込みたいときがあるとおもいます。

そんなときに使えるスクリプトを作りましたので公開します。

機能概要

  • ファイルで指定した複数のブランチ、引数で指定したブランチをマージする。
  • マージ前にリモートリポジトリから最新をpullする。
  • 競合が発生した場合など、マージに失敗した場合はマージ前の状態に戻す。

スクリプト

動作確認環境

Windows10 + Git for Windows + Git Bash

スクリプト本体

auto_merge.sh
#!/bin/bash

# ./auto_merge.sh ./ develop ./branch_list.txt

if [ $# -ne 3 ]
then
  echo "usage : ${0##*/} <repository_path> <merge_commit> <target_branch_list_path>"
  exit 1
else
  REPO=$1
  SHA=$2
  LIST=$3
fi

cd ${REPO}

# 対象ブランチファイル読み込み
while read BRANCH
do
  echo "
  ${BRANCH}"

  git checkout ${BRANCH}
  if [ $? -ne 0 ]
  then
    # 対象ブランチなし
    echo "fail to checkout"
    continue
  fi

  git pull
  git merge ${SHA}
  if [ $? -ne 0 ]
  then
    # マージ失敗 元に戻す
    echo "${BRANCH} is fail"
    git merge --abort
  fi
done < ${LIST}

マージ先ブランチリスト

branch_list.txt
feature/func1
feature/func2

1行につき1ブランチ記述する
改行はLF

実行方法

> auto_merge.sh <repository_path> <merge_commit> <target_branch_list_path>
# <repository_path>:リポジトリのパス
# <merge_commit>:マージ元(branch or commit)
# <target_branch_list_path>:マージ先ブランチリストのパス

実行例

実行前状態

実行前状態

実行

$ ./auto_merge.sh . develop branch_list.txt

  feature/func1
Already on 'feature/func1'
fatal: No remote repository specified.  Please, specify either a URL or a
remote name from which new revisions should be fetched.
Merge made by the 'recursive' strategy.
 .gitignore | 1 -
 1 file changed, 1 deletion(-)

  feature/func2
Switched to branch 'feature/func2'
fatal: No remote repository specified.  Please, specify either a URL or a
remote name from which new revisions should be fetched.
Merge made by the 'recursive' strategy.
 .gitignore | 1 -
 1 file changed, 1 deletion(-)

リモートリポジトリがない場合はgitのエラーメッセージが出ますがそのままマージします。

実行後状態

実行後状態

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0