Skip to content

Instantly share code, notes, and snippets.

@reatlat
Forked from dciccale/git_branch.sh
Created July 4, 2021 19:17
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 reatlat/b3e99e26cf33f01b0aa1b9d4cc750eff to your computer and use it in GitHub Desktop.
Save reatlat/b3e99e26cf33f01b0aa1b9d4cc750eff to your computer and use it in GitHub Desktop.
Bash script to get the current git branch and last commit
#!/usr/bin/env bash
# checks if branch has something pending
function parse_git_dirty() {
git diff --quiet --ignore-submodules HEAD 2>/dev/null; [ $? -eq 1 ] && echo "*"
}
# gets the current git branch
function parse_git_branch() {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1$(parse_git_dirty)/"
}
# get last commit hash prepended with @ (i.e. @8a323d0)
function parse_git_hash() {
git rev-parse --short HEAD 2> /dev/null | sed "s/\(.*\)/@\1/"
}
# DEMO
GIT_BRANCH=$(parse_git_branch)$(parse_git_hash)
echo ${GIT_BRANCH}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment