Skip to content

Instantly share code, notes, and snippets.

@slezica
Last active August 7, 2019 14:29
Show Gist options
  • Save slezica/c2b5e5a8dbd610d306a14bc6c130cd8e to your computer and use it in GitHub Desktop.
Save slezica/c2b5e5a8dbd610d306a14bc6c130cd8e to your computer and use it in GitHub Desktop.

Hey there!

This utility requires 3 things:

  • The fzf tool installed
  • A small git custom command
  • 4 lines of bash in your bashrc

First off, the fzf tool. You can get it from https://github.com/junegunn/fzf . It's an amazing utility to have in your belt, and I've found multiple uses for it over time.

Second, a custom command that uses fzf to show an interactive git history, much like tig. The key here is that, when you press enter, the script finishes outputting the hash of the commit you selected. I call this git-inspect.

#!/bin/bash

format='format:%C(dim white bold)%h%Creset %s'
preview='git show -m {1} | tail -n+6 | diff-so-fancy'

git log --color=always --pretty="$format" \
  | fzf --ansi --no-sort --height=100% --preview-window=right:70% --preview="$preview" \
  | cut -d ' ' -f 1

Note: I'm using diff-so-fancy here, but any pager is alright. The default diff format is probably not.

The next piece is a generic bash helper that allows you to insert the result of a command right into the line you're editing without submitting the input. Another utility with many uses.

run-insert-inline() {
    local selected=$(eval "$1" | xargs)
    READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}"
    READLINE_POINT=$(( READLINE_POINT + ${#selected} ))
}

Finally, a binding to combine both scripts and work the magic. In my case, alt+o:

bind -x '"\eo": run-insert-inline "git inspect"'

The final result: you can type...

$ git-revise <alt+o>

... select a commit while looking at the diff, press enter and voilá. History edited.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment