# If you work with git, you've probably had that nagging sensation of not knowing what branch you are on. Worry no longer! | |
export PS1="\\w:\$(git branch 2>/dev/null | grep '^*' | colrm 1 2)\$ " | |
# This will change your prompt to display not only your working directory but also your current git branch, if you have one. Pretty nifty! | |
# ~/code/web:beta_directory$ git checkout master | |
# Switched to branch "master" | |
# ~/code/web:master$ git checkout beta_directory | |
# Switched to branch "beta_directory" | |
# ~/code/web:beta_directory$ |
mxdpeep
commented
Oct 11, 2022
via email
function parse_git_dirty {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working tree clean" ]] && echo ""
}
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/ (.*)/(\1$(parse_git_dirty))/"
}
export PS1='[\e[\033[01;34m]\u@\h [\e[38;5;211m]\W[\e[\033[38;5;48m]$(parse_git_branch)[\e[\033[00m]$ '
he11 yeah! This color scheme is hands down the best on the internet. No contest. If this was mortal kombat this color code whispers "finish them"
I edited a bit @vankasteelj's stuff and made something like zsh, to react to git's changes
If there are some changes then the color changes to orange or whatever, like in zsh (or fish also, I don't know) so, to do that, you need to create a function
function changes_in_branch() { if [ -d .git ]; then if expr length + "$(git status -s)" 2>&1 > /dev/null; then echo -ne "\033[0;33m$(__git_ps1)\033[0m"; else echo -ne "\033[0;32m$(__git_ps1)\033[0m"; fi; fi }and then simply just add that function to the
PS1
stuff. Just like this:PS1='\[\033[0;32m\]\[\033[0m\033[0;32m\]\u\[\033[0;36m\] @ \[\033[0;36m\]\h \w\[\033[0m\]$(changes_in_branch)\n\[\033[0;32m\]└─\[\033[0m\033[0;32m\] \$\[\033[0m\033[0;32m\] ▶\[\033[0m\] '
Works perfect in a Laptop with WSL and Ubuntu. I recently moved to a Macbook with M1, tried to use the same logic unsucessfully. After some tweaks I edited the changes_in_branch()
function to make it work in my Mac. This is the result:
function changes_in_branch() {
if [ -d .git ]; then
ChangesStr="$(git status -s)"
if [[ -n $ChangesStr ]]; then
echo -ne "\033[0;33m$(__git_ps1)\033[0m";
else
echo -ne "\033[0;32m$(__git_ps1)\033[0m"; fi;
fi
}
I had also to install bash-git-prompt in order to make work __git_ps1