Wednesday, July 23, 2014

Updating Mac command-line prompt for Git

I recently enrolled in a Data Science class with General Assembly in DC, in order to  gain some hands on experience towards my goal of solving meaningful real-world problems leveraging data and data science.

For our homework assignments, we were required to install and use Git and GitHub.

In class, I discovered that Windows users running Git Bash got some extra built-in Git functionality in their shell command prompt whereby it showed them their current branch when in a Git repository. I did not see this on my Mac and thus set out to determine how I configure this, knowing that Mac command prompts are configurable.

I googled the topic this and found a post titled "Creating a Happy Git Environment on OS X" by Trey Piepmeier which gave some steps to follow under the section titled "Bash Fanciness". 

I followed the steps ...
  1. Edit the bash profile:
        vi ~/.bash_profile
  2. Add the following lines to the bash profile and save: 
  3.     source /usr/local/git/contrib/completion/git-completion.bash
        GIT_PS1_SHOWDIRTYSTATE=true
        export PS1='[\u@mbp \w$(__git_ps1)]\$ '
  4. Source the bash profile:
        . ~/.bash_profile

... but found I was getting 2 errors:

      1. Could not locate the file /usr/local/git/contrib/completion/git-completion.bash:
         -bash: /usr/local/git/contrib/completion/git-completion.bash:
                    No such file or directory
      2. Could not find the command __git_ps1:
         -bash: __git_ps1: command not found

I needed to verify where Git was installed, so I could see if the required file was in a different directory. That's when I realized my Mac already had Git installed as part of the OS X operating system, but it was an older version and despite me having installed the latest version of Git with Homebrew, my Mac was still using the older preinstalled version. After some more reading, I discovered I had to set my $PATH variable in my bash profile to include /usr/local/bin.

Steps to fix ...
  1. Edit the bash profile:
  2.     vi ~/.bash_profile
  3. Add the following lines to the bash profile and save:
  4.     export PATH=/usr/local/bin:$PATH
  5. Source the bash profile:
  6.     . ~/.bash_profile
  7. Verify that you're using the most recent version of Git:
  8.     git --version

    I also modified my bash profile to source the Homebrew installed version of git-completion.bash, which I found under /usr/local/share/zsh/site-functions/git-completion.bash:

  9. Edit the bash profile:
  10.     vi ~/.bash_profile
  11. Change the following line: 
  12.     source /usr/local/git/contrib/completion/git-completion.bash
    to:
        source /usr/local/share/zsh/site-functions/git-completion.bash
  13. Source the bash profile:
  14.     . ~/.bash_profile

    This eliminated the first error, but I was still getting the second error:
        -bash: __git_ps1: command not found

    After digging around, I then found that the Mac OS X version of the bash shell was also not the most recent, so I decided to update that too.

  15. Update bash shell via Homebrew:
  16.     brew install bash

    I had to add the path of this new bash shell to the /etc/shells file and then change the login shell for the current user to this version too:

  17. Edit /etc/shells file:
  18.     sudo vi /etc/shells
  19. Add path of new shell to the end of the file and save:
  20.     /usr/local/bin/bash
  21. Run chsh command to change the login shell of the current user to the newly installed version:
  22.     chsh -s /usr/local/bin/bash [user]
  23. Restart your terminal application and verify your bash version by doing the following:
  24.     bash --version
  25. After further research, I found that I should also source .git-prompt.sh in my bash profile:
  26.     vi ~/.bash_profile
  27. Add the following line and save:
  28.     source /usr/local/Cellar/git/2.0.1/etc/bash_completion.d/git-prompt.sh
  29. Source the bash profile:
  30.     . ~/.bash_profile
All of that, just to get a Git friendly command prompt! While I'm sure I did not need to do all the updates, just to get the prompt to work, it made sense to get them done and I learned a lot in the process.

 

Sources:

Lindsey, Buddy. “Adding Git Data to Your Bash Prompt Buddy Lindsey. Buddy Lindsey LLC, 14 May. 2013. Web. 23 Jul. 2014.
Yarin. my fat llama. “Can't edit /etc/profile on my mac superuser. StackExchange, 28 Feb. 2011. Web. 23 Jul. 2014.
Which Git? How to Keep Git Up To Date on Mac Exploring Front–end Web Development & Design. Gray Ghost Visuals Press  19 Feb. 2013. Web. 23 Jul. 2014.
Piepmeier, Trey. “Creating a Happy Git Environment on OS X GitHub Gist. GitHub, Inc.  17 May. 2012. Web. 23 Jul. 2014.

No comments: