Srijan R. Shetty bio photo

Srijan R. Shetty

Eccentric, Scribbler, Hacker

Email LinkedIn Github RSS

Sometimes while fixing multiple bugs related to a feature branch you might come across the peculiar situation that you want to add some more files to an older commit. Hitherto, my workflow was a patchwork of creating a new commit with squash! prefix and then clumsily performing an interactive rebase to position the commit on the correct SHA.

Fortunately, I’ve found a much better flow which greatly simplifies development. Two tools are critical in helping me with this workflow. git being the usual suspect, but fzf is the secret sauce. I have the following snippet in my zsh sourced files:

function is_in_git_repo() {
    git rev-parse HEAD > /dev/null 2>&1
}

function fzf-down() {
    fzf --height 50% "[email protected]" --border
}

function gh() {
    is_in_git_repo || return
    git log --date=short --format="%C(green)%C(bold)%cd %C(auto)%h%d %s (%an)" --graph --color=always |
        fzf-down --ansi --no-sort --reverse --multi --bind 'ctrl-s:toggle-sort' \
        --header 'Press CTRL-S to toggle sort' \
        --preview 'grep -o "[a-f0-9]\{7,\}" <<< {} | xargs git show --color=always | head -'$LINES |
        grep -o "[a-f0-9]\{7,\}"
    }

function bind-git-helper() {
    local c
    for c in [email protected]; do
        eval "fzf-g$c-widget() { local result=\$(g$c | join-lines); zle reset-prompt; LBUFFER+=\$result }"
        eval "zle -N fzf-g$c-widget"
        eval "bindkey '^k^$c' fzf-g$c-widget"
    done
}
bind-git-helper h
unset -f bind-git-helper

This allows me to hit h in a project managed by git which opens up a view