DEV Community

Discussion on: Productivity 101: Git Aliases for Lazy Developers

Collapse
 
mojemoron profile image
Micheal Ojemoron

yeah, I understand the angle you are coming from. Uncoupling git aliases from the shell environment is a better way to go, thanks for the feedback. Nice git aliases :)

Collapse
 
190245 profile image
Dave

My full .gitconfig (sanitised), if you're interested. Steal/borrow whatever you feel might be useful.

pastebin.com/wPavbDnJ

(and yes, my Bash Aliases has "g=git", so I can do things like g acp "commit msg")

Thread Thread
 
mojemoron profile image
Micheal Ojemoron

Lol 😂 Nicee! I will borrow some 😉

Thread Thread
 
190245 profile image
Dave

Since sharing is caring, I also have the branch (and some identifiers) on my terminal prompt.

.bashrc:

function parse_git_branch() {
    BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
    if [ ! "${BRANCH}" == "" ]
    then
        STAT=`parse_git_dirty`
        echo " (${BRANCH}${STAT})"
    else
        echo ""
    fi
}
function parse_git_dirty {
    status=`git status 2>&1 | tee`
    dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
    untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
    ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
    newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
    renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
    deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
    bits=''
    if [ "${renamed}" == "0" ]; then
        bits=">${bits}"
    fi
    if [ "${ahead}" == "0" ]; then
        bits="*${bits}"
    fi
    if [ "${newfile}" == "0" ]; then
        bits="+${bits}"
    fi
    if [ "${untracked}" == "0" ]; then
        bits="?${bits}"
    fi
    if [ "${deleted}" == "0" ]; then
        bits="x${bits}"
    fi
    if [ "${dirty}" == "0" ]; then
        bits="!${bits}"
    fi
    if [ ! "${bits}" == "" ]; then
        echo " ${bits}"
    else
        echo ""
    fi
}

export PS1="[\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\]] $ "

Thread Thread
 
mojemoron profile image
Micheal Ojemoron

Wow, what does this script do?

Thread Thread
 
190245 profile image
Dave • Edited

Obviously don't add it if you don't understand it (never trust a random on the internet).

But to answer your question, this:
Terminal Image

On any terminal prompt in a git repository, I see username/hostname/path (as normal), but then the git branch (in yellow) followed by some "bit flags". In the picture, the exclamation mark tells me I have modified some file(s), the question mark tells me I've added some file(s). Just after a commit, the bit flags disappear to tell me that the working directory is "clean."

There's other flags, as described in the script. If I'm not in a git repo, I just see the normal terminal.