0
0

gitのブランチ移動を簡単にする

Last updated at Posted at 2023-06-06

はじめに

gitでバージョン管理をしている
GUIの操作を覚えたくないのでCLIでやろうとするが長くなりがちなブランチ名をいちいちコピペするのもめんどくさい
というところで、ブランチの移動とやりたいときはマージまでコマンドで簡単にできないかとシェルスクリプトを作成した。
これでブランチの移動にストレスがなくなりました
./bashrcへの追加でさらに使いやすくなります
何か意見改善点あればぜひ教えてください!!

目次

  1. 制作品
  2. スクリプトの説明
  3. .bashrcへの登録
  4. 参考文献

制作品

  1. 完成品
    git_checkout_by_number.sh
    #!/bin/bash
    
    # helpの表示
    if [ "$1" = '-h' ]; then
      echo 'usage gcn <opt> <num>'
      echo ''
      echo 'opt'
      echo ' -h show help'
      echo ' -m integrate before into after'
      echo ' -p pull the branch after move'
      echo 'num'
      echo ' input the branch num'
      exit 1
    fi
    
    branches=($(git for-each-ref --format='%(refname:short)' refs/heads/))
    
    be_branch_name=($(git rev-parse --abbrev-ref HEAD))
    
    echo "Available branches:"
    for i in "${!branches[@]}"; do
      printf "%3d" "$i"
      echo ": ${branches[$i]}"
    done
    
    
    
    if [[ "$1" =~ ^[0-9]+$ ]]; then
      branch_number=$1
    else
      if [[ "$2" =~ ^[0-9]+$ ]]; then
        branch_number=$2
      else
        read -p "Enter branch number to checkout: " branch_number
    
        if [ -z "$branch_number" ] || [ $branch_number = "q" ]; then
          echo "Invalid branch number."
          exit 1
        fi
      fi
    fi
    
    af_branch_name=${branches[$branch_number]}
    echo "chosed branch: <" $af_branch_name ">"
    if [ -z "$af_branch_name" ]; then
      echo "Invalid branch number."
      exit 1
    else
      echo ">> git checkout $af_branch_name"
      git checkout $af_branch_name
    fi
    
    if [[ "$1" =~ [p] ]]; then
      # 引数がある場合は、引数の値を出力
      echo ">> git pull origin $af_branch_name"
      git pull origin $af_branch_name
    fi
    
    if [[ "$1" =~ [m] ]]; then
      # 引数がある場合は、引数の値を出力
      echo ">> git merge $be_branch_name"
      git merge $be_branch_name
    fi
    
  2. 使い方
    $ ./git_checkout_by_number.sh
      Available branches:
       0: develop
       1: feature/branch1
       2: feature/branch2
      Enter branch number to checkout: 1
      Switched to branch 'feature/branch1'
    

シェルスクリプトを実行するとブランチの一覧が表示され、数字を指定することで、そのブランチに移動することができる。
./git_checkout_by_number.sh -m
のように引数をとにかく何か入れることで、ブランチの移動をした後にその前にいたブランチがマージされる

スクリプトの説明

ブランチの取得と表示

数字が二桁になっても揃うようにしている

branches=($(git for-each-ref --format='%(refname:short)' refs/heads/))

echo "Available branches:"
for i in "${!branches[@]}"; do
  printf "%3d" "$i"
  echo ": ${branches[$i]}"
done

入力の取得と例外処理

間違えた時とか用に入力なしまたはqを入力することで実行が止まるようにしている

read -p "Enter branch number to checkout: " branch_number

if [ -z "$branch_number" ] || [ $branch_number = "q" ]; then
  echo "Invalid branch number."
  exit 1
fi

ブランチの取得とcheckout

移動先のブランチ名を取得し、あれば移動するようにする.
数字であれば範囲外のものを入れるとちゃんとエラーとなってくれるが、例えばaとか入れると0のブランチに飛ぶ
なぜ??

af_branch_name=${branches[$branch_number]}

if [ -z "$af_branch_name" ]; then
  echo "Invalid branch number."
else
  git checkout $af_branch_name
fi

マージの処理

あらかじめ取得してあった現在のブランチをマージするように
引数を指定してもいいが、表記揺れがあっても実行するように今回はとにかく引数があればこれが実行されることとした

be_branch_name=($(git rev-parse --abbrev-ref HEAD))
...
if [ $# -gt 0 ]; then
  # 引数がある場合は、引数の値を出力
  git merge $be_branch_name
fi

.bashrcへの登録

gitは比較的どこでも使うので./bashrcに登録したほうが使いやすいです
いちいちプロジェクトごとに置いとくのもignoreするのもめんどいですしね
自分の場合はgit_checkout_by_number.sh/usr/local/bin/mine/において、
.bashrcに下記を追記しました。

./bashrc
export PATH=/usr/local/bin/mine/:$PATH
alias gcbn='git_checkout_by_number'
$ gcbn

で利用できます

参考文献

ごめんなさい
あったかもしれませんが忘れました
参考にした方ありがとうございました

0
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
0
0