How to show the git HEAD hash in bash?

Viewed 44

I was able to show my current branch in my bash (see photo below) by following the instructions from How do I show the git branch with colours in Bash prompt?.

enter image description here

But I would like to ask if its possible we can append the git HEAD hash beside of the branch?

// format
user@user: directory(CURRENT_BRANCH)(CURRENT_HEAD_HASH)

// looks like
user@user: directory(feature/user_profile_page_be)(2d00c77)
1 Answers

I was able to show the hash and changed the color. Check here for color reference

force_color_prompt=yes
color_prompt=yes
parse_git_branch() {
 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
parse_rev_parse() {
 git rev-parse --short HEAD
}
if [ "$color_prompt" = yes ]; then
 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\033[01;32m\]($(parse_rev_parse))\[\033[00m\]\$ '
fi
unset color_prompt force_color_prompt

enter image description here

Related