{"id":34,"date":"2010-04-08T03:55:12","date_gmt":"2010-04-08T01:55:12","guid":{"rendered":"http:\/\/arnorehn.de\/cgi-bin\/weblog_basic\/index.php?p=34"},"modified":"2016-08-07T12:00:20","modified_gmt":"2016-08-07T10:00:20","slug":"current-git-branch-in-shell-prompt","status":"publish","type":"post","link":"https:\/\/www.arnorehn.de\/blog\/2010\/04\/08\/current-git-branch-in-shell-prompt\/","title":{"rendered":"Current git branch in shell prompt"},"content":{"rendered":"<p>Since I&#8221;ve read much about git and how great it is, I recently thought that I had to try it myself. And indeed, it is quite amazing and is nice to work with. There was one thing that I disliked, though: I never quite knew in which branch of my repository I was, especially after I&#8221;ve been away from coding for a few hours.<br \/>\nOf course I could just look that up, but I tend to forget that and then end up with code that belongs to a different branch. Now the shell prompt looked like a good place to show the current branch, but simply executing &#8220;git branch | grep ^*&#8221; would only work in the top-level directory of the repository. So I wrote the following function which will traverse all containing directories and look for a .git directory. Having found one, it will nicely print the the branch in dark green color, with a @ sign in front of it:<\/p>\n<p><strong>Update:<\/strong> As many people have pointed out, bash-completion provides a function called __git_ps1 which will happily print the current git branch and can be used as well. I don&#8221;t quite like the rest of bash-completion, though, so I&#8221;ll stick with my own.<br \/>\nAs Yuri pointed out, git branch works from any directory, so here&#8221;s the updated (and much shorter \ud83d\ude42 ) version:<\/p>\n<pre>\nfunction formattedGitBranch {\n    _branch=\"$(git branch 2>\/dev\/null | sed -e \"\/^\\s\/d\" -e \"s\/^\\*\\s\/\/\")\"\n    test -n \"$_branch\" && echo -e \" @\\e[0;32m $_branch\"\n}\n<\/pre>\n<p>Having this in my .bashrc, I could integrate it quite nicely with my prompt:<\/p>\n<pre>\\[\\e[1;32m\\]\\u\\[\\e[m\\] \\[\\e[1;34m\\]\\w\\[\\e[m\\]$(formattedGitBranch) \\[\\e[1;32m\\]\\$ \\[\\e[m\\]\\[\\e[0m\\]<\/pre>\n<p>Screenshot:<br \/>\n<img decoding=\"async\" src=\"http:\/\/www.arnorehn.de\/var\/git-prompt.png\" alt=\"git prompt\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since I&#8221;ve read much about git and how great it is, I recently thought that I had to try it myself. And indeed, it is quite amazing and is nice to work with. There was one thing that I disliked, though: I never quite knew in which branch of my repository I was, especially after [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[8,28],"class_list":["post-34","post","type-post","status-publish","format-standard","hentry","category-other","tag-bash","tag-git"],"_links":{"self":[{"href":"https:\/\/www.arnorehn.de\/blog\/wp-json\/wp\/v2\/posts\/34","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.arnorehn.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.arnorehn.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.arnorehn.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.arnorehn.de\/blog\/wp-json\/wp\/v2\/comments?post=34"}],"version-history":[{"count":1,"href":"https:\/\/www.arnorehn.de\/blog\/wp-json\/wp\/v2\/posts\/34\/revisions"}],"predecessor-version":[{"id":161,"href":"https:\/\/www.arnorehn.de\/blog\/wp-json\/wp\/v2\/posts\/34\/revisions\/161"}],"wp:attachment":[{"href":"https:\/\/www.arnorehn.de\/blog\/wp-json\/wp\/v2\/media?parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.arnorehn.de\/blog\/wp-json\/wp\/v2\/categories?post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.arnorehn.de\/blog\/wp-json\/wp\/v2\/tags?post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}