LoginSignup
4
0

More than 1 year has passed since last update.

Gitのブランチ名をエディタで簡単に編集できるコマンド

Last updated at Posted at 2017-08-23

みなさんは、git checkout -b でブランチを作ってから「アッ!スペルミスしてる!」と気づいた経験はありませんか?私はよくあります。

そんな時に、

$ git branch feature/162514-shitdown-chrome-api feature/162514-shutdown-chrome-api

のようにターミナル上で長いブランチ名を全て入力し直すのは面倒です。

ターミナルではなくお気に入りのエディタで、間違えた部分だけ直せれば、便利でしょう。

エディタでブランチ名を編集できるコマンド

$ git be

を実行すると、エディタが起動しブランチ名を編集できます。保存してエディタを終了すると、ブランチ名が書き換わります。

使うエディタは、環境変数$EDITORで指定してください(私はMicroを使っています)。

以下のソースを、~/bin/git-be などに保存してください。もちろん git be というコマンド名を変えたい場合は、別の名前でも問題ありません。

#!/bin/bash
branch_name=$(git rev-parse --abbrev-ref HEAD)
if [ "$branch_name" = 'HEAD' ]; then
  echo 'A non-branch revision is currently checked out.' >&2
  exit 1
fi

t="$(mktemp "temp.git-be.XXXXXX")"
trap 'rm -f "$t"' 0 1 2 3 15

echo "$branch_name" >> "$t"
e="${EDITOR:-vi}"
"$e" "$t"
exit_status=$?
if [ $exit_status -ne 0 ]; then
  echo "Editor $e exited with code $exit_status"
  exit 1
fi

new_branch_name="$(cat "$t")"
git branch -m "$new_branch_name" || exit 1
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