Skip to content

Instantly share code, notes, and snippets.

@thomasandersen77
Forked from dciccale/git_branch.sh
Created January 29, 2016 09:40
Show Gist options
  • Select an option

  • Save thomasandersen77/35fa917f4df4f890802c to your computer and use it in GitHub Desktop.

Select an option

Save thomasandersen77/35fa917f4df4f890802c to your computer and use it in GitHub Desktop.

Revisions

  1. @dciccale dciccale created this gist May 11, 2013.
    20 changes: 20 additions & 0 deletions git_branch.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    #!/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}