Fortes


Make Git better with FZF

How to look like a cool movie hacker while using Git

Lake Chelan

Lake Chelan Green’s Landing, Washington

Like any nerd who spends way too much time in the command-line, I absolutely love FZF, a fantastic fuzzy finder that’s been an essential part of my workflow for years now.

By default, FZF doesn’t do much but it really shines when you use it in conjunction with other tools. I use it on a near-daily basis with Git, here’s the relevant section of my .gitconfig file, the full file is available in my dotfiles repo:

# Place in `~/.gitconfig` or `~/.config/git/config`
[alias]
  addm = "!git ls-files --deleted --modified --other --exclude-standard | fzf -0 -m --preview 'git diff --color=always {-1}' | xargs -r git add"
  addmp = "!git ls-files --deleted --modified --exclude-standard | fzf -0 -m --preview 'git diff --color=always {-1}' | xargs -r -o git add -p"
  cb = "!git branch --all | grep -v '^[*+]' | awk '{print $1}' | fzf -0 --preview 'git show --color=always {-1}' | sed 's/remotes\\/origin\\///g' | xargs -r git checkout"
  cs = "!git stash list | fzf -0 --preview 'git show --pretty=oneline --color=always --patch \"$(echo {} | cut -d: -f1)\"' | cut -d: -f1 | xargs -r git stash pop"
  db = "!git branch | grep -v '^[*+]' | awk '{print $1}' | fzf -0 --multi --preview 'git show --color=always {-1}' | xargs -r git branch --delete"
  Db = "!git branch | grep -v '^[*+]' | awk '{print $1}' | fzf -0 --multi --preview 'git show --color=always {-1}' | xargs -r git branch --delete --force"
  ds = "!git stash list | fzf -0 --preview 'git show --pretty=oneline --color=always --patch \"$(echo {} | cut -d: -f1)\"' | cut -d: -f1 | xargs -r git stash drop"
  edit = "!git ls-files --modified --other --exclude-standard | sort -u | fzf -0 --multi --preview 'git diff --color {}' | xargs -r $EDITOR -p"
  fixup = "!git log --oneline --no-decorate --no-merges | fzf -0 --preview 'git show --color=always --format=oneline {1}' | awk '{print $1}' | xargs -r git commit --fixup"
  resetm = "!git diff --name-only --cached | fzf -0 -m --preview 'git diff --color=always {-1}' | xargs -r git reset"

Here’s what each command does, along with a demo for a few of them:

  • addm: Add new or modified files, use <tab> to select multiple, patch mode via addmp. Preview shows the file diff.

  • cb: Select branch to switch to, preview shows last commit

    Using FZF to switch git branches
  • cs: Choose stash to apply, preview shows diff

  • db: Delete branches, use <tab> to select multiple, force delete via Db. Preview shows last commit

    Using FZF to delete git branches
  • ds: Delete a stash, preview shows diff

  • edit: Choose modified files to edit, use <tab> to select multiple (one tab per file). Preview shows diff

    Using FZF to delete git branches
  • fixup: Commit staged files as a fixup, fuzzy-finding the original commit. Preview shows commit

  • resetm: Choose staged files to reset, use <tab> to select multiple. Preview shows cached diff

The FZF wiki has a few more examples if you’re looking for further inspiration. Hopefully, this will change your life as much as it did mine. Just make sure you don’t scream out “I love you, FZF!” during an intimate moment just like … a friend of mine did. He lives in Canada, you wouldn’t know him.