在 bash shell 讓 git 自動補齊 branch name

NickWarm
Nickwarm Journey
Published in
2 min readMay 18, 2019
Photo by Austin Distel on Unsplash

過去,我會先用 git branch 查看有哪些 branch 再滑鼠反白複製貼上 branch name,來切換 branch

但這樣實在有點慢,比較簡單的做法是,在 terminal 裡下這段指令

curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash

然後去 ~/.bash_profile 加上這段並儲存

[[ -f "$HOME/.git-completion.bash" ]] && source "$HOME/.git-completion.bash"

接著記得在 terminal 裡用 source ~/.bash_profile 來重讀 ~/.bash_profile

假如我有一個名叫 login-password-mail 的 branch,只要在打完 git checkout 後按 lo 再按 tab

就會自動補齊成

git checkout login-password-mail

此外,也能切換 remote 的 branch,i.e:

我想要切換到 fix-active_subscriptions 這 branch 假如我只打 fi 再按 tab,可以發現有數個選擇

這時字就要打多一點,例如打到 fix-a 才會補齊成我想要的 fix-active_subscriptions

References

--

--

NickWarm
Nickwarm Journey

Rubyist。Nicholas aka Nick. Experienced in Ruby, Rails. I like to share the experiences what I learned.