Optimizing your workflow Git-Bash-101

Git is a developer’s best friend. When ever you screw up, git is always there to save the day. Here are some cool bash scripts to optimize your git workflow. Add the following snippets to your .bash_profile and voila.

1.Know where you are.

Often you forget which branch you are working on. It will be great if you always knew which branch you are on currently.

function current_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \1/'

export PS1="\u@\h \W\[\033[32m\] |\$(current_branch)\[\033[00m\] > "

2.Push this!

Especially when working with colleagues who use weird and complicated branch names, typing the name every time you push code is a pain in the ‘wrong’ place.

function gpthis(){
    cowsay "pushing $(current_branch) to $remote"
    git push $remote $(current_branch)

3.First to push.

When multiple people are working in the same branch, there is always a race for who pushes first. The guy who pushed second, has to pull rebase and then push, too much work right?

function gpsafe(){
    git stash
    conflict=$(git pull -r $remote $(current_branch) | grep -i "CONFLICT")

    if [[ -n $conflict ]]
        cowsay "Please resolve the above CONFLICTS"
        git status
        gpthis $remote
    git stash pop


Written by
Nithin Krishna
Published at
Jun 11, 2014
Posted in
If you want to get more posts like this, join our newsletter