{"id":936,"date":"2010-08-08T18:43:19","date_gmt":"2010-08-08T16:43:19","guid":{"rendered":"http:\/\/raftaman.net\/?p=936"},"modified":"2021-05-15T11:46:40","modified_gmt":"2021-05-15T09:46:40","slug":"display-current-git-branch-in-bash","status":"publish","type":"post","link":"https:\/\/possiblelossofprecision.net\/?p=936","title":{"rendered":"Display current git branch in bash"},"content":{"rendered":"<p>Sometimes it&#8217;s very useful to know which git branch you are working on right from the command prompt. There are many solutions out there, but most of them include python and some awk or grep magic which can time a serious amount of time when you cd into a reasonably large git tree.<\/p>\n<p>But you can also take advantage of the <code>__git_ps1<\/code> function, provided by <code>\/etc\/bash_completion.d\/git<\/code> in the git package. Add this line to <strong><code>~\/.bashrc<\/code><\/strong>:<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\nexport PS1='&#x5B;\\u@\\h \\W$(__git_ps1 &quot; (%s)&quot;)]\\$ '\r\n<\/pre>\n<p>This setting helps you keep track of which branch you are in at a given time. If you are in a git working directory, it shows the current branch as part of the prompt:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;user@host directory-name (master)]$ \r\n<\/pre>\n<p>If you do not have the <a href=\"http:\/\/bash-completion.alioth.debian.org\/\"><strong>bash-completion<\/strong><\/a> package installed, you must manually source the git completion script prior to using <code>__git_ps1()<\/code>. To do this, add <\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\nsource \/etc\/bash_completion.d\/git\r\n<\/pre>\n<p> to <code>~\/.bashrc<\/code>.<\/p>\n<p>You might also want to display when there are changes in your work tree or the git index:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;user@host directory-name (master*)]$ \r\n&#x5B;user@host directory-name (master+)]$ \r\n&#x5B;user@host directory-name (master%)]$ \r\n<\/pre>\n<ul>\n<li>* indicates that a tracked file was modified<\/li>\n<li>+ indicates that a tracked file was modified and staged (with git add)<\/li>\n<li>% indicates that you have untracked files in your tree <\/li>\n<\/ul>\n<p>To do so, simply add these lines in your <code>~\/.bashrc<\/code>, right before the line modifying your prompt:<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\nexport GIT_PS1_SHOWDIRTYSTATE=true\r\nexport GIT_PS1_SHOWUNTRACKEDFILES=true\r\n<\/pre>\n<p>See the comments at the beginning of <code>\/etc\/bash_completion.d\/git<\/code> for more details. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes it&#8217;s very useful to know which git branch you are working on right from the command prompt. There are many solutions out there, but most of them include python and some awk or grep magic which can time a serious amount of time when you cd into a reasonably large git tree. But you can also take advantage of&#8230; <a href=\"https:\/\/possiblelossofprecision.net\/?p=936\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[28,7,36],"class_list":["post-936","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-bash","tag-fedora","tag-git"],"_links":{"self":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/936","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=936"}],"version-history":[{"count":8,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/936\/revisions"}],"predecessor-version":[{"id":2679,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/936\/revisions\/2679"}],"wp:attachment":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}