{"id":8386,"date":"2012-09-24T20:50:00","date_gmt":"2012-09-24T15:20:00","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8386"},"modified":"2016-12-19T14:50:37","modified_gmt":"2016-12-19T09:20:37","slug":"modify-promt-to-make-it-git-specific","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/modify-promt-to-make-it-git-specific\/","title":{"rendered":"Modify promt to make it GIT specific."},"content":{"rendered":"<p>We use git a lot, and one of the most used commands is &#8220;git branch&#8221;. Few Months back we found a script that enabled us to modify our prompt so that it shows our current branch. We have modified it further to add extra functionality, and our modified script enables prompt to show number of files modified, number untracked files, and also the new files along with the branch name.<\/p>\n<p>Below is  sample image from terminal.<\/p>\n<p>In image the prompt shows same information briefly as  shown by git status. Here <\/p>\n<ol>\n<li> U shows Untracked files.<\/li>\n<li>M shows modified files <\/li>\n<li> And NEW Shows new file that has been added to Git<\/li>\n<\/ol>\n<p>And below is the actual script.<\/p>\n<p>[shell]<br \/>\nGST=&amp;quot;git status&amp;quot;<br \/>\nOLDPS1=$PS1 <\/p>\n<p>function parse_git_branch {<br \/>\n\tgit branch 2&amp;amp;gt; \/dev\/null | sed -e &#8216;\/^[^*]\/d&#8217; -e &#8216;s\/* \\(.*\\)\/[\\1] \/&#8217;<br \/>\n}<\/p>\n<p>function git_status() {<br \/>\n    \tif current_git_status=$($GST 2&amp;amp;gt; \/dev\/null | grep &#8216;modified&#8217;); then<br \/>\n\t\tCOUNT=`git status |grep &amp;quot;modified&amp;quot; | wc -l| sed -e &#8216;s\/^[ \\t]*\/\/&#8217;`<br \/>\n\t\techo &amp;quot;[M x $COUNT]&amp;quot;<br \/>\n\tfi<br \/>\n}<\/p>\n<p>function git_untracked(){<br \/>\n    \tif current_git_status=$($GST 2&amp;amp;gt; \/dev\/null | grep &#8216;Untracked files:&#8217; ); then<br \/>\n     \t\techo &amp;quot;[U]&amp;quot;<br \/>\n    \tfi<br \/>\n}<br \/>\nfunction new_file_check(){<br \/>\n\tif git_dir=$(git branch 2&amp;amp;gt; \/dev\/null | grep &#8216;*&#8217; ); then<br \/>\n                COUNTNEW=`git status |  grep &amp;quot;new file:&amp;quot; | wc -l| sed -e &#8216;s\/^[ \\t]*\/\/&#8217;`<br \/>\n                if [ $COUNTNEW -gt 0 ]; then<br \/>\n                echo &amp;quot;[New x $COUNTNEW]&amp;quot;<br \/>\n\t\tfi<\/p>\n<p>        fi<br \/>\n}<\/p>\n<p>function ren_file_check(){<br \/>\n        if git_dir=$(git branch 2&amp;amp;gt; \/dev\/null | grep &#8216;*&#8217; ); then<br \/>\n                COUNTREN=`git status |  grep &amp;quot;renamed:&amp;quot; | wc -l| sed -e &#8216;s\/^[ \\t]*\/\/&#8217;`<br \/>\n                if [ $COUNTREN -gt 0 ]; then<br \/>\n                echo &amp;quot;[Renamed x $COUNTREN]&amp;quot;<br \/>\n                fi<\/p>\n<p>        fi<br \/>\n}<\/p>\n<p>function git_hash(){<br \/>\n\tif git_dir=$(git branch 2&amp;amp;gt; \/dev\/null | grep &#8216;*&#8217; ); then<br \/>\n\t\techo &amp;quot;[`git log -1 &#8211;oneline | cut -d&amp;quot; &amp;quot; -f1 `]&amp;quot;<br \/>\n\tfi<br \/>\n}<\/p>\n<p>function gitfyPrompt(){<br \/>\n\tif updatePrompt=$(git branch 2&amp;amp;gt; \/dev\/null | grep &#8216;*&#8217; ); then<br \/>\n\techo &amp;quot;   ###############################################&amp;quot;<br \/>\n\techo &amp;quot;   #            Welcome to Git Console           #&amp;quot;<br \/>\n\techo &amp;quot;   #   Use restorePrompt to restore your prompt  #&amp;quot;<br \/>\n\techo &amp;quot;   ###############################################&amp;quot;<br \/>\n\tPS1=&amp;quot;\\[\\e[1;31m\\]Git-Console \\[\\e[0m\\] \\W : \\$(new_file_check)\\$(ren_file_check)\\$(git_status)\\$(git_untracked)\\$(parse_git_branch)$ &amp;quot;<br \/>\n\telse<br \/>\n\techo &amp;quot;WORKS ONLY IN GIT REPO&amp;quot;<br \/>\n\tfi<\/p>\n<p>}<\/p>\n<p>function restorePrompt(){<br \/>\n\techo &amp;quot;   ###############################################&amp;quot;<br \/>\n        echo &amp;quot;   #               Exiting Git Console           #&amp;quot;<br \/>\n        echo &amp;quot;   ###############################################&amp;quot;<\/p>\n<p>PS1=$OLDPS1<br \/>\n}<\/p>\n<p>gitfyPrompt<br \/>\n[\/shell]<\/p>\n<p>This really saves tons of time and is pretty handy  for a quick reference.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We use git a lot, and one of the most used commands is &#8220;git branch&#8221;. Few Months back we found a script that enabled us to modify our prompt so that it shows our current branch. We have modified it further to add extra functionality, and our modified script enables prompt to show number of [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1],"tags":[375,1051,822],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8386"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8386"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8386\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}