2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Linux TerminalにGitのBranch名を表示

Last updated at Posted at 2023-10-06

はじめに

Terminalのコマンドラインに現在のGitのブランチ名を表示するように環境設定する方法です。

設定方法

① 「~/.bashrc」または「~/.bash_profile」に、以下のGithub GistのURLを参考してコードを追記してください。
色などの変更は自由にすれば良いです。

私の場合は、以下のように少しコードをカスタムして、「~/.bashrc」に追記しました。

~/.bashrc
function parse_git_branch () {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
 
RED="\[\033[01;31m\]"
YELLOW="\[\033[01;33m\]"
GREEN="\[\033[01;32m\]"
BLUE="\[\033[01;34m\]"
NO_COLOR="\[\033[00m\]"
PS1="$GREEN\u@\h$NO_COLOR:$BLUE\w $RED\$(parse_git_branch)$NO_COLOR\$ "

② sourceコマンドを使用して修正した「~/.bashrc」または「~/.bash_profile」を適用してください。

$ source ~/.bashrc

③ Gitを使用できるパスに、ブランチ名が表示されます。

USER@HOST:~/test/test-repository (main)$
2
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?