LoginSignup
0
0

More than 3 years have passed since last update.

peco を使ってブランチのチェックアウトを行う (リモートブランチ と ローカルブランチ) 改良版

Last updated at Posted at 2020-10-19

peco を使ってブランチのチェックアウトを行う (リモートブランチ と ローカルブランチ) 改良版

https://qiita.com/m-tmatma/items/8285a6cace98cfdb5db9 の改良版です。

改善版1

git checkout $(git branch -a | grep -v "/HEAD" | peco | sed -r "s#^\\s+remotes/origin/##")

改善ポイント

https://qiita.com/tommy_aka_jps/items/6ce68f564cb3802ca9b0 の記事で以下で proj/exampleWorks のリモートブランチをチェックアウトできるとあったのでそれを反映して簡略化しました。

git checkout proj/exampleWorks
  • git branch -a でリモートブランチとリモートブランチを両方列挙します。
  • sed で git branch -a を使ったときにリモート名のところを削除します。
  • git checkout proj/exampleWorks でリモートブランチをチェックアウトできることを利用します。

改善版2 (git リポジトリの外で実行した場合に git checkout を実行しない版)

alias co='git rev-parse --is-inside-git-dir > /dev/null 2>&1 \
  && git checkout $(git branch -a | grep -v "/HEAD" | peco | sed -r "s#^\\s+remotes/origin/##" | sed -r "s#^\*\s+##")'

改善ポイント

  • git リポジトリの外で実行した場合に git checkout を実行しない
  • 現在いるブランチを選択してもエラーにならないようにする。
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