Last Updated: February 25, 2016
·
572
· joeheyming

Tab complete your recent git commits

If you like bash-completion, you can make your own. I've been tinkering around with making a bash completion to make it easier to push a specific git commit. My old workflow for pushing a specific commit used to be this:

  1. rebase, move commits around
  2. git log, see the commit id, copy the id
  3. git push origin <commit-id>:master

I got tired of doing this, so I created this bash completion: https://gist.github.com/joeheyming/5a3892afda9db4789987

function pushCommit() {
    git push origin $1:master;
}
function _pushCommit() {
    local RECENT_COMMITS=`git log --pretty=oneline master..HEAD`

    COMPREPLY=()
    local cur
    cur=$(_get_cword)

    local OLDIFS="$IFS"
    local IFS=$'\n'
    COMPREPLY=( $( compgen -W "$RECENT_COMMITS" -- "$cur" ) )
    IFS="$OLDIFS"
    if [[ ${#COMPREPLY[*]} -eq 1 ]]; then #Only one completion
        COMPREPLY=( ${COMPREPLY[0]%% *} ) #Remove ' ' and everything after
    fi
    return 0
}
complete -F _pushCommit pushCommit

Now I just type my function:
bash pushCommit <<TAB>> 1804c645dcbac9dd361c29ae1531ff96e8 Fixed the bug. 6690badf47395ecde6fd7d8d81fbf8a0a7 Super cool new feature.

and I see the most recent commits on my branch up to my local master. (with the subject of the commit)

Try it out! What do you think? I'd like to know if you have similar issues with other git commands.