0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GIT チェックアウトブランチの入力がめんどくさいので

Last updated at Posted at 2025-05-20

naze

複数に開発しているときに
ブランチどこにいるのか
またプルリクとか確認のたびにブランチを切り替えるのがめんどくさかった
(GUI的に切り替えるやつ使えばよかったかも)
ので少しでも楽にしたい

らくにしたやつ

以下コードを~/.bashrcに追記

vi ~/.bashrc

bashrcに追記する内容

# ブランチ名表示やつ
if [ $UID -eq 0 ]; then
    PS1='\[\033[31m\]\u@\h\[\033[00m\]:\[\033[01m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\\$ '
else
    PS1='\[\033[36m\]\u@\h\[\033[00m\]:\[\033[01m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\\$ '
fi

# ブランチ簡単チェックアウトしたいなやつ
gitco() {
  local branches branch index
  branches=($(git branch --sort=-committerdate | sed 's/^\*//' | sed 's/^ //'))
  for i in "${!branches[@]}"; do
    printf "%2d: %s\n" "$i" "${branches[$i]}"
  done

  if [ -n "$1" ]; then
    index="$1"
    branch="${branches[$index]}"
    if [ -n "$branch" ]; then
      git checkout "$branch"
    else
      echo "Invalid index: $index"
    fi
  fi
}

bashrc再読み込み
ログインログアウトでも可

source ~/.bashrc

一覧表示

(main)$ gitco

 0: main
 1: feature/branchA
 2: feaarui/branchB
 3: branchC#2234
 4: branchD/kozin
 5: branchE/kojin

コマンド+IDでブランチへスイッチ

(main)$ gitco 3

Switched to branch 'branchC#2234'
Your branch is behind 'origin/main' by 10 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

(branchC#2234)$ 

まとめ

gitco        # 番号付きで一覧表示
gitco 1      # インデックス番号でチェックアウト
0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?