---
title: "２段bashとGitブランチ表示をやる"
id: "2723"
type: "post"
slug: "post-2723"
published_at: "2020-02-26T12:07:15+00:00"
modified_at: "2023-04-27T10:41:44+00:00"
url: "https://www.okin-jp.net/archives/2723"
markdown_url: "https://www.okin-jp.net/archives/2723.md"
excerpt: "MacのWindowsAppからxrdpにrdp接続するとusキーボードになってしまうので調査 Continue reading"
taxonomy_category:
  - "Linux"
  - "PC"
  - "Server"
  - "その他"
taxonomy_post_tag:
  - "Bash"
  - "Git"
  - "PowerShell"
  - "Windows"
---

## Ubuntu 18.04の場合

.bashrcの末尾に追記

Shell

# source ~/.git-prompt.sh if [ -f /etc/bash_completion ]; then . /etc/bash_completion fi if [ -f /etc/bash_completion.d/git-prompt ]; then export PS1='\[\033[01;32m\]\u@\h\[\033[01;33m\] \w$(__git_ps1) \n\[\033[01;34m\]\$\[\033[00m\] ' else export PS1='\[\033[01;32m\]\u@\h\[\033[01;33m\] \w \n\[\033[01;34m\]\$\[\033[00m\] ' fi

| 123456789 | # source ~/.git-prompt.shif [ -f /etc/bash_completion ]; then. /etc/bash_completionfiif [ -f /etc/bash_completion.d/git-prompt ]; thenexport PS1='\[\033[01;32m\]\u@\h\[\033[01;33m\] \w$(__git_ps1) \n\[\033[01;34m\]\$\[\033[00m\] 'elseexport PS1='\[\033[01;32m\]\u@\h\[\033[01;33m\] \w \n\[\033[01;34m\]\$\[\033[00m\] 'fi |
| --- | --- |

## Windowsの場合(PowerShell)

事前準備としてPowerShell(管理者)で、下記のコマンドを実行する

PowerShell

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

| 1 | Set-ExecutionPolicy -ExecutionPolicy RemoteSigned |
| --- | --- |

C:\Users\<ユーザー名>\Documents\WindowsPowerShell\Profile.ps1 を作成して書き込む

(PowerShellの$profileに詳しければそれに合わせて追記することも可能)

PowerShell

function Get-GitBranch{ git branch 2>$null | ForEach-Object { if($_[0] -eq "*") { $branch = ($_ -Split " ")[1] return "(git:$branch)" } } } function prompt { (Get-Host).UI.RawUI.WindowTitle = "Windows PowerShell $pwd" $name = $pwd.Path $gb = Get-GitBranch Write-Host "" -NoNewLine -ForegroundColor White Write-Host $env:USERNAME -NoNewLine -ForegroundColor Cyan Write-Host "@$name " -NoNewLine -ForegroundColor White Write-Host "$gb" -ForegroundColor Red Write-Host ">" -NoNewLine -ForegroundColor White return " " }

| 123456789101112131415161718192021 | function Get-GitBranch{ git branch 2>$null \| ForEach-Object { if($_[0] -eq "*") { $branch = ($_ -Split " ")[1] return "(git:$branch)" } }}function prompt { (Get-Host).UI.RawUI.WindowTitle = "Windows PowerShell $pwd" $name = $pwd.Path $gb = Get-GitBranch Write-Host "" -NoNewLine -ForegroundColor White Write-Host $env:USERNAME -NoNewLine -ForegroundColor Cyan Write-Host "@$name " -NoNewLine -ForegroundColor White Write-Host "$gb" -ForegroundColor Red Write-Host ">" -NoNewLine -ForegroundColor White return " "} |
| --- | --- |

参考

[Ubuntu bashのプロンプトHack!!!!（gitブランチ表示）https://qiita.com/ryosukue/items/bc1eae639e3950790eb9](https://qiita.com/ryosukue/items/bc1eae639e3950790eb9)
 (2020/01/05閲覧)

[PowerShellのプロンプトにGitのブランチを表示したくなった](https://m0t0k1x2.tumblr.com/post/132379609864/powershell%E3%81%AE%E3%83%97%E3%83%AD%E3%83%B3%E3%83%97%E3%83%88%E3%81%ABgit%E3%81%AE%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81%E3%82%92%E8%A1%A8%E7%A4%BA%E3%81%97%E3%81%9F%E3%81%8F%E3%81%AA%E3%81%A3%E3%81%9F)
[https://m0t0k1x2.tumblr.com/post/132379609864/powershell%E3%81%AE%E3%83%97%E3%83%AD%E3%83%B3%E3%83%97%E3%83%88%E3%81%ABgit%E3%81%AE%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81%E3%82%92%E8%A1%A8%E7%A4%BA%E3%81%97%E3%81%9F%E3%81%8F%E3%81%AA%E3%81%A3%E3%81%9F](https://m0t0k1x2.tumblr.com/post/132379609864/powershell%E3%81%AE%E3%83%97%E3%83%AD%E3%83%B3%E3%83%97%E3%83%88%E3%81%ABgit%E3%81%AE%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81%E3%82%92%E8%A1%A8%E7%A4%BA%E3%81%97%E3%81%9F%E3%81%8F%E3%81%AA%E3%81%A3%E3%81%9F)
 (2020/01/05閲覧)

[PowerShell で Profile を利用して スクリプトの自動読み込みをしてみよう https://tech.guitarrapc.com/entry/2013/09/23/164357](https://tech.guitarrapc.com/entry/2013/09/23/164357)
(2020/01/05閲覧)

![おきん(blog)](https://secure.gravatar.com/avatar/90fa8845304183a491e9f3bfa0a6a44e46e25882407e4aa652815d725d55bd8c?s=100&r=g)

[おきん(blog)](https://www.okin-jp.net/archives/author/oops-okin)

[https://mi.okin-jp.net/@okin_p](https://mi.okin-jp.net/@okin_p)
(Misskey)  
 [https://fi.okin-jp.net/@okin_p](https://fi.okin-jp.net/@okin_p)
(Firefish)  
 [https://si.okin-jp.net/@okin_p](https://si.okin-jp.net/@okin_p)
(Mastodon)

[https://fedifile.net/@okin_p/profile/](https://fedifile.net/@okin_p/profile/)
  
 [https://twpf.jp/okin_p](https://twpf.jp/okin_p)
  
 [http://profile.hatena.ne.jp/okin_p/](http://profile.hatena.ne.jp/okin_p/)

名前：おきん  
 やってること：面倒なことをPHPにやらせる、IPv6でなにかする、車で移動する  
 主なガジェット：Google Pixel 4a (5G) ,iPhone XS(ジャンク), iPad mini5, その他

種別：IPv6 Single stack 猫

パソコンやガジェットについて書いていく…予定。

[https://twitter.com/okin_p](https://twitter.com/okin_p)
[https://si.okin-jp.net/@okin_p](https://si.okin-jp.net/@okin_p)

### *関連記事*

### コメントを残す[コメントをキャンセル](/archives/2723#respond)

このサイトはスパムを低減するために Akismet を使っています。[コメントデータの処理方法の詳細はこちらをご覧ください](https://akismet.com/privacy/)
。
