Skip to content

Instantly share code, notes, and snippets.

@seivan
Forked from zaps166/.bashrc
Created May 27, 2020 11:36
Show Gist options
  • Save seivan/c1ae9f2830a9b0b29a9bfb45257c1c9c to your computer and use it in GitHub Desktop.
Save seivan/c1ae9f2830a9b0b29a9bfb45257c1c9c to your computer and use it in GitHub Desktop.
Display Git commit hash and branch name and enable git completions
getGitInfo()
{
checksum=$(git rev-parse --short HEAD 2> /dev/null)
branch=$(git symbolic-ref --short HEAD 2> /dev/null)
if [ ! -z $branch ] && [ ! -z $checksum ]; then
echo -e " ($branch $checksum)"
elif [ ! -z $checksum ]; then
echo -e " ($checksum)"
elif [ ! -z $branch ]; then
echo -e " ($branch)"
fi
}
if [ $EUID == 0 ] ; then
PS1='\[\033[01;31m\][$? \u@\h\[\033[01;36m\] \w\[\033[01;31m\]]\[\033[01;35m\]$(getGitInfo)\[\033[01;31m\]\$\[\033[00m\] '
else
PS1='\[\033[01;32m\][$? \u@\h\[\033[01;37m\] \w\[\033[01;32m\]]\[\033[01;35m\]$(getGitInfo)\[\033[01;32m\]\$\[\033[00m\] '
fi
OS=$(uname)
if [ $OS == "Darwin" ]; then
GIT_COMPLETION_PATH="/Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash"
if [[ ! -e $GIT_COMPLETION_PATH ]]; then
GIT_COMPLETION_PATH="/Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash"
fi
else
GIT_COMPLETION_PATH="/usr/share/git/completion/git-completion.bash"
fi
[ -r $GIT_COMPLETION_PATH ] && . $GIT_COMPLETION_PATH
@seivan
Copy link
Author

seivan commented May 27, 2020

Also works git name-rev HEAD --name-only for branch not sure what the difference is.

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