Skip to content

Instantly share code, notes, and snippets.

@grese
Last active October 9, 2019 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grese/a101913c00cc2120bd0d0cd4e9a6359f to your computer and use it in GitHub Desktop.
Save grese/a101913c00cc2120bd0d0cd4e9a6359f to your computer and use it in GitHub Desktop.
Automatic git bash prompt setup for MacOS
#!/bin/bash
#=======================================
# git-prompt-setup.sh
#
# Adds git branch to your bash prompt.
#=======================================
GITPROMPT_DIR=~/.bin
GITPROMPT_FILE=git-prompt.sh
GITPROMPT_URL=https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
BASH_PROFILE_FILE=~/.bash_profile
# Fetch and save the git-prompt shell script
mkdir -p $GITPROMPT_DIR
curl -o $GITPROMPT_DIR/$GITPROMPT_FILE $GITPROMPT_URL
# Write the new prompt to bash profile
cat > $BASH_PROFILE_FILE <<- EOM
#### Git Prompt ####
PS1='\h:\W \u$(__git_ps1 "(%s)")\$'
EOM
# Update the prompt:
source ~/.bash_profile
echo "Your prompt has been updated. To modify the prompt, edit the PS1 variable in ~/.bash_profile"
@grese
Copy link
Author

grese commented Oct 9, 2019

Description:

Automatically modifies your bash prompt to include the current git branch of your current working directory.

  • Creates the ~/.bin directory (if it doesn't exist already)
  • Downloads Git's git-prompt.sh script, and places it in ~/.bin
  • Modifies the bash prompt (PS1) in .bash_profile

When inside a git repository, the format of the prompt will be user@host:directory(branch)$. When not in a git repo, the prompt will be user@host:directory$

Usage:

$ ./git-prompt-setup.sh

NOTE: this script could easily be modified to work for bash on any OS just by changing the "BASH_PROFILE_FILE" variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment