{"id":2119,"date":"2024-06-17T16:16:43","date_gmt":"2024-06-17T14:16:43","guid":{"rendered":"https:\/\/robermb.com\/blog\/?p=2119"},"modified":"2024-06-17T16:18:57","modified_gmt":"2024-06-17T14:18:57","slug":"how-to-show-the-git-branch-in-the-terminal-prompt-on-macos","status":"publish","type":"post","link":"https:\/\/robermb.com\/blog\/geeks\/how-to-show-the-git-branch-in-the-terminal-prompt-on-macos\/","title":{"rendered":"How to Show the Git Branch in the Terminal Prompt on macOS"},"content":{"rendered":"\n<p>If you&#8217;re a developer working with Git on macOS, it can be <strong>incredibly<\/strong> <strong>helpful<\/strong> <strong>to have your current Git branch<\/strong> displayed right in your <strong>terminal<\/strong> <strong>prompt<\/strong>. This guide will walk you through the steps to set this up.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Open Your Terminal<\/h3>\n\n\n\n<p>First, open the Terminal application on your Mac. You can find it in <code>Applications &gt; Utilities<\/code> or by searching for &#8220;Terminal&#8221; in Spotlight.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Edit Your Bash Profile<\/h3>\n\n\n\n<p>If you&#8217;re using the default shell in macOS, which is Zsh (macOS Catalina and later), you will need to edit the <code>.zshrc<\/code> file. For older versions of macOS using Bash, you will edit the <code>.bash_profile<\/code> file.<\/p>\n\n\n\n<p>To open the file for editing, use the following commands:<\/p>\n\n\n\n<ul>\n<li>For Zsh (macOS Catalina and later):<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ vi ~\/.zshrc<\/code><\/pre>\n\n\n\n<ul>\n<li>For Bash (macOS Mojave and earlier):<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ vi ~\/.bash_profile<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Add the Git Branch Display Code<\/h3>\n\n\n\n<p>Next, you need to add a function to display the current Git branch in your prompt. Depending on the shell you are using, add the corresponding code snippet to the file you opened.<\/p>\n\n\n\n<ul>\n<li>For Zsh, add the following:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># Git branch in prompt\nautoload -Uz vcs_info\nprecmd() { vcs_info }\nsetopt prompt_subst\nPROMPT='%F{yellow}%n@%m%f %1~ %F{green}${vcs_info_msg_0_}%f %# '\nzstyle ':vcs_info:git:*' formats '(%b)'\nzstyle ':vcs_info:*' actionformats '(%b)'<\/code><\/pre>\n\n\n\n<ul>\n<li>For Bash, add the following:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># Git branch in prompt.\nparse_git_branch() {\n    git branch 2&gt; \/dev\/null | sed -e '\/^&#91;^*]\/d' -e 's\/* \\(.*\\)\/ (\\1)\/\n}\nexport PS1=\"\\u@\\h \\W\\&#91;\\033&#91;32m\\]\\$(parse_git_branch)\\&#91;\\033&#91;00m\\] $ \"<\/code><\/pre>\n\n\n\n<p>Other option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Git branch in prompt\nparse_git_branch() {\n  git branch 2&gt;\/dev\/null | grep '*' | sed 's\/* \/\/'\n}\nPS1=\"\\u@\\h \\w \\&#91;\\033&#91;32m\\]\\$(parse_git_branch)\\&#91;\\033&#91;00m\\] $ \"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Apply the Changes<\/h3>\n\n\n\n<p>After adding the code, save the file and exit the editor.<\/p>\n\n\n\n<p>To apply the changes, you need to reload the profile (or close the session \/ Terminal and reopen it). Use the following command based on the file you edited:<\/p>\n\n\n\n<ul>\n<li>For Zsh:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ source ~\/.zshrc<\/code><\/pre>\n\n\n\n<ul>\n<li>For Bash:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ source ~\/.bash_profile<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Verify the Changes<\/h3>\n\n\n\n<p>Now, <strong>navigate<\/strong> to <strong>any Git repository<\/strong> using the <code>cd<\/code> command, and <strong>you should see the current Git branch<\/strong> name displayed in your terminal prompt. For example, if you&#8217;re on the <code><strong>master<\/strong><\/code> <strong>branch<\/strong>, your prompt might look something like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>robermb@MacBook-Pro lab_ansible (master) $<\/code><\/pre>\n\n\n\n<p>And that&#8217;s it! You&#8217;ve successfully configured your terminal to display the current Git branch in the prompt on macOS.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re a developer working with Git on macOS, it can be incredibly helpful to have your current Git branch &hellip; <a href=\"https:\/\/robermb.com\/blog\/geeks\/how-to-show-the-git-branch-in-the-terminal-prompt-on-macos\/\" class=\"more-link\">More <span class=\"screen-reader-text\">How to Show the Git Branch in the Terminal Prompt on macOS<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,5],"tags":[126,138,36],"_links":{"self":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/2119"}],"collection":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/comments?post=2119"}],"version-history":[{"count":5,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/2119\/revisions"}],"predecessor-version":[{"id":2130,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/2119\/revisions\/2130"}],"wp:attachment":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/media?parent=2119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/categories?post=2119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/tags?post=2119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}